{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:01:35.115112Z",
     "start_time": "2019-11-28T18:01:34.923922Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Populating the interactive namespace from numpy and matplotlib\n"
     ]
    }
   ],
   "source": [
    "%pylab inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "ulab is a C module for micropython. My goal was to implement a small subset of numpy. I chose those functions and methods that might be useful in the context of a microcontroller. This means low-level data processing of linear (array) and two-dimensional (matrix) data.\n",
    "\n",
    "The main points of ulab are \n",
    "\n",
    "- compact, iterable and slicable container of numerical data in 1, and 2 dimensions (arrays and matrices). In addition, these containers support all the relevant unary and binary operators (e.g., `len`, ==, +, *, etc.)\n",
    "- vectorised computations on micropython iterables and numerical arrays/matrices (universal functions)\n",
    "- basic linear algebra routines (matrix inversion, matrix reshaping, and transposition)\n",
    "- polynomial fits to numerical data\n",
    "- fast Fourier transforms\n",
    "\n",
    "The code itself is split into submodules. This should make exclusion of unnecessary functions, if storage space is a concern. Each section of the implementation part kicks out with a short discussion on what can be done with the particular submodule, and what are the tripping points at the C level. I hope that these musings can be used as a starting point for further discussion on the code.\n",
    "\n",
    "The code and its documentation can be found under https://github.com/v923z/micropython-ulab/. The MIT licence applies to all material."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Environmental settings and magic commands\n",
    "\n",
    "The entire C source code, as well as the documentation (mainly verbose comments on certain aspects of the implementation) are contained in this notebook. The code is exported to separate C files in `/ulab/`, and then compiled from this notebook. However, I would like to stress that the compilation does not require a jupyter notebook. It can be done from the command line by invoking the command in the [make](#make), or [Compiling the module](#Compiling-the-module). After all, the ipython kernel simply passes the `make` commands to the underlying operating system.\n",
    "\n",
    "Testing is done on the unix and stm32 ports of micropython, also directly from the notebook. This is why this section contains a couple of magic functions. But once again: the C module can be used without the notebook. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:01:44.329018Z",
     "start_time": "2019-11-28T18:01:44.325091Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Errno 2] No such file or directory: '../../micropython/ports/unix/'\n",
      "/home/v923z/sandbox/micropython/v1.11/micropython/ports/unix\n"
     ]
    }
   ],
   "source": [
    "%cd ../../micropython/ports/unix/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:01:47.359162Z",
     "start_time": "2019-11-28T18:01:47.355619Z"
    }
   },
   "outputs": [],
   "source": [
    "from IPython.core.magic import Magics, magics_class, line_cell_magic\n",
    "from IPython.core.magic import cell_magic, register_cell_magic, register_line_magic\n",
    "from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring\n",
    "import subprocess\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 804,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T17:55:30.378218Z",
     "start_time": "2019-09-18T17:55:30.371467Z"
    }
   },
   "outputs": [],
   "source": [
    "def string_to_matrix(string):\n",
    "    matrix = []\n",
    "    string = string.replace(\"array(\\'d\\', \", '').replace(')', '').replace('[', '').replace(']', '')\n",
    "    for _str in string.split('\\r\\n'):\n",
    "        if len(_str) > 0:\n",
    "            matrix.append([float(n) for n in _str.split(',')])\n",
    "    return array(matrix)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## micropython magic command\n",
    "\n",
    "The following magic class takes the content of a cell, and depending on the arguments, either passes it to the unix, or the stm32 implementation. In the latter case, a pyboard must be connected to the computer, and must be initialised beforehand. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:01:52.242940Z",
     "start_time": "2019-11-28T18:01:52.195351Z"
    }
   },
   "outputs": [],
   "source": [
    "@magics_class\n",
    "class PyboardMagic(Magics):\n",
    "    @cell_magic\n",
    "    @magic_arguments()\n",
    "    @argument('-skip')\n",
    "    @argument('-unix')\n",
    "    @argument('-file')\n",
    "    @argument('-data')\n",
    "    @argument('-time')\n",
    "    @argument('-memory')\n",
    "    def micropython(self, line='', cell=None):\n",
    "        args = parse_argstring(self.micropython, line)\n",
    "        if args.skip: # doesn't care about the cell's content\n",
    "            print('skipped execution')\n",
    "            return None # do not parse the rest\n",
    "        if args.unix: # tests the code on the unix port. Note that this works on unix only\n",
    "            with open('/dev/shm/micropython.py', 'w') as fout:\n",
    "                fout.write(cell)\n",
    "            proc = subprocess.Popen([\"./micropython\", \"/dev/shm/micropython.py\"], \n",
    "                                    stdout=subprocess.PIPE, stderr=subprocess.PIPE)\n",
    "            print(proc.stdout.read().decode(\"utf-8\"))\n",
    "            print(proc.stderr.read().decode(\"utf-8\"))\n",
    "            return None\n",
    "        if args.file: # can be used to copy the cell content onto the pyboard's flash\n",
    "            spaces = \"    \"\n",
    "            try:\n",
    "                with open(args.file, 'w') as fout:\n",
    "                    fout.write(cell.replace('\\t', spaces))\n",
    "                    printf('written cell to {}'.format(args.file))\n",
    "            except:\n",
    "                print('Failed to write to disc!')\n",
    "            return None # do not parse the rest\n",
    "        if args.data: # can be used to load data from the pyboard directly into kernel space\n",
    "            message = pyb.exec(cell)\n",
    "            if len(message) == 0:\n",
    "                print('pyboard >>>')\n",
    "            else:\n",
    "                print(message.decode('utf-8'))\n",
    "                # register new variable in user namespace\n",
    "                self.shell.user_ns[args.data] = string_to_matrix(message.decode(\"utf-8\"))\n",
    "        \n",
    "        if args.time: # measures the time of executions\n",
    "            pyb.exec('import utime')\n",
    "            message = pyb.exec('t = utime.ticks_us()\\n' + cell + '\\ndelta = utime.ticks_diff(utime.ticks_us(), t)' + \n",
    "                               \"\\nprint('execution time: {:d} us'.format(delta))\")\n",
    "            print(message.decode('utf-8'))\n",
    "        \n",
    "        if args.memory: # prints out memory information \n",
    "            message = pyb.exec('from micropython import mem_info\\nprint(mem_info())\\n')\n",
    "            print(\"memory before execution:\\n========================\\n\", message.decode('utf-8'))\n",
    "            message = pyb.exec(cell)\n",
    "            print(\">>> \", message.decode('utf-8'))\n",
    "            message = pyb.exec('print(mem_info())')\n",
    "            print(\"memory after execution:\\n========================\\n\", message.decode('utf-8'))\n",
    "\n",
    "        else:\n",
    "            message = pyb.exec(cell)\n",
    "            print(message.decode('utf-8'))\n",
    "\n",
    "ip = get_ipython()\n",
    "ip.register_magics(PyboardMagic)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### pyboard initialisation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-24T18:07:26.532228Z",
     "start_time": "2019-09-24T18:07:26.398434Z"
    },
    "hidden": true
   },
   "outputs": [],
   "source": [
    "import pyboard\n",
    "pyb = pyboard.Pyboard('/dev/ttyACM0')\n",
    "pyb.enter_raw_repl()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### pyboad detach"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "hidden": true
   },
   "outputs": [],
   "source": [
    "pyb.exit_raw_repl()\n",
    "pyb.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T05:56:41.168185Z",
     "start_time": "2019-09-27T05:56:41.162486Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "        IPython.CodeCell.options_default.highlight_modes['magic_text/x-csrc'] = {'reg':[/^\\s*%%ccode/]};\n",
       "    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "    (function () {\n",
       "        var defaults = IPython.CodeCell.config_defaults || IPython.CodeCell.options_default;\n",
       "        defaults.highlight_modes['magic_text/x-csrc'] = {'reg':[/^\\s*%%makefile/]};\n",
       "    })();\n",
       "    "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import IPython\n",
    "\n",
    "js = \"\"\"\n",
    "    (function () {\n",
    "        var defaults = IPython.CodeCell.config_defaults || IPython.CodeCell.options_default;\n",
    "        defaults.highlight_modes['magic_text/x-csrc'] = {'reg':[/^\\\\s*%%ccode/]};\n",
    "    })();\n",
    "    \"\"\"\n",
    "cjs = \"\"\"\n",
    "        IPython.CodeCell.options_default.highlight_modes['magic_text/x-csrc'] = {'reg':[/^\\\\s*%%ccode/]};\n",
    "    \"\"\"\n",
    "\n",
    "IPython.core.display.display_javascript(cjs, raw=True)\n",
    "\n",
    "js = \"\"\"\n",
    "    (function () {\n",
    "        var defaults = IPython.CodeCell.config_defaults || IPython.CodeCell.options_default;\n",
    "        defaults.highlight_modes['magic_text/x-csrc'] = {'reg':[/^\\\\s*%%makefile/]};\n",
    "    })();\n",
    "    \"\"\"\n",
    "IPython.core.display.display_javascript(js, raw=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code magic\n",
    "\n",
    "The following cell magic simply writes a licence header, and the contents of the cell to the file given in the header of the cell. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:01:58.976516Z",
     "start_time": "2019-11-28T18:01:58.968120Z"
    }
   },
   "outputs": [],
   "source": [
    "@magics_class\n",
    "class MyMagics(Magics):\n",
    "        \n",
    "    @cell_magic\n",
    "    def ccode(self, line, cell):\n",
    "        copyright = \"\"\"/*\n",
    " * This file is part of the micropython-ulab project, \n",
    " *\n",
    " * https://github.com/v923z/micropython-ulab\n",
    " *\n",
    " * The MIT License (MIT)\n",
    " *\n",
    " * Copyright (c) 2019 Zoltán Vörös\n",
    "*/\n",
    "    \"\"\"\n",
    "        if line:\n",
    "            with open('../../../ulab/code/'+line, 'w') as cout:\n",
    "                cout.write(copyright)\n",
    "                cout.write(cell)\n",
    "            print('written %d bytes to %s'%(len(copyright) + len(cell), line))\n",
    "            return None\n",
    "\n",
    "ip = get_ipython()\n",
    "ip.register_magics(MyMagics)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# Notebook conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1007,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:33:57.223059Z",
     "start_time": "2019-10-08T15:33:57.216411Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/v923z/sandbox/micropython/v1.11/ulab/docs\n"
     ]
    }
   ],
   "source": [
    "%cd ../../../ulab/docs/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1008,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:34:02.561400Z",
     "start_time": "2019-10-08T15:33:59.273974Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/v923z/anaconda3/lib/python3.7/site-packages/nbconvert/filters/datatypefilter.py:41: UserWarning: Your element with mimetype(s) dict_keys(['application/javascript']) is not able to be represented.\n",
      "  mimetypes=output.keys())\n"
     ]
    }
   ],
   "source": [
    "import nbformat as nb\n",
    "import nbformat.v4.nbbase as nb4\n",
    "from nbconvert import RSTExporter\n",
    "\n",
    "def convert_notebook(nbfile, rstfile):\n",
    "    (rst, resources) = rstexporter.from_filename(nbfile)\n",
    "    with open(rstfile, 'w') as fout:\n",
    "        fout.write(rst)\n",
    "        \n",
    "rstexporter = RSTExporter()\n",
    "rstexporter.template_file = './templates/rst.tpl'\n",
    "\n",
    "convert_notebook('ulab.ipynb', './source/ulab.rst')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "# Compiling the module\n",
    "\n",
    "Detailed instructions on how to set up and compile a C module can be found in chapter 2 of https://micropython-usermod.readthedocs.io/en/latest/. \n",
    "\n",
    "First, on the command line, you should clone both the micropython, and the `ulab` repositories: \n",
    "\n",
    "```bash\n",
    "git clone https://github.com/micropython/micropython.git\n",
    "```\n",
    "Then navigate to your micropython folder, and run \n",
    "\n",
    "```bash\n",
    "git clone https://github.com/v923z/micropython-ulab.git ulab\n",
    "```\n",
    "\n",
    "Finally, in the `mpconfigport.h` header file of the port that you want to compile for, you have to define the variable `MODULE_ULAB_ENABLED`\n",
    "\n",
    "```make\n",
    "#define MODULE_ULAB_ENABLED (1)\n",
    "```\n",
    "\n",
    "At this point, you should be able to run make in the port's root folder:\n",
    "\n",
    "```bash\n",
    "make USER_C_MODULES=../../../ulab all\n",
    "```\n",
    "(unix port) or \n",
    "```bash\n",
    "make BOARD=PYBV11 CROSS_COMPILE=<Path where you uncompressed the toolchain>/bin/arm-none-eabi-\n",
    "```\n",
    "(pyboard). When compiling for the pyboard (or any other hardware platform), you might or might not have to set the cross-compiler's path. If your installation of the cross-compiler is system-wide, you can drop the `make` argument `CROSS_COMPILE`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# The ndarray type"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## General comments\n",
    "\n",
    "`ndarrays` are efficient containers of numerical data of the same type (i.e., signed/unsigned chars, signed/unsigned integers or floats). Beyond storing the actual data, the type definition has three additional members (on top of the `base` type). Namely, two `size_t` objects, `m`, and `n`, which give the dimensions of the matrix (obviously, if the `ndarray` is meant to be linear, either `m`, or `n` is equal to 1), as well as the byte size, `bytes`, i.e., the total number of bytes consumed by the data container. `bytes` is equal to `m*n` for `byte` types (`uint8`, and `int8`), to `2*m*n` for integers (`uint16`, and `int16`), and `4*m*n` for floats. \n",
    "\n",
    "The type definition is as follows:\n",
    "\n",
    "```c\n",
    "typedef struct _ndarray_obj_t {\n",
    "    mp_obj_base_t base;\n",
    "    size_t m, n;\n",
    "    mp_obj_array_t *array;\n",
    "    size_t bytes;\n",
    "} ndarray_obj_t;\n",
    "```\n",
    "\n",
    "**NOTE: with a little bit of extra effort, mp_obj_array_t can be replaced by a single void array. We should, perhaps, consider the pros and cons of that. One patent advantage is that we could get rid of the verbatim copy of array_new function in ndarray.c. On the other hand, objarray.c facilities couldn't be used anymore.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Handling different types\n",
    "\n",
    "In order to make the code type-agnostic, we will resort to macros, where necessary. This will inevitably add to the firmware size, because, in effect, we unroll the code for each possible case. However, the source will be much more readable. Also note that by unrolling, we no longer need intermediate containers and we no longer need to dispatch type-conversion functions, which means that we should be able to gain in speed."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Additional structure members in numpy\n",
    "\n",
    "Also note that, in addition, `numpy` defines the following members:\n",
    "\n",
    "- `.ndim`: the number of dimensions of the array (in our case, it would be 1, or 2)\n",
    "- `.size`: the number of elements in the array; it is the product of m, and n\n",
    "- `.dtype`: the data type; in our case, it is basically stored in data->typecode\n",
    "- `.itemsize`: the size of a single element in the array: this can be gotten by calling `mp_binary_get_size('@', data->typecode, NULL)`.\n",
    "\n",
    "One should, perhaps, consider, whether these are necessary fields. E.g., if `ndim` were defined, then \n",
    "\n",
    "```c\n",
    "if((myarray->m == 1) || (myarray->n == 1)) {\n",
    "    ...\n",
    "}\n",
    "```\n",
    "\n",
    "could be replaced by \n",
    "\n",
    "```c\n",
    "if(myarray->ndim == 1) {\n",
    "    ...\n",
    "}\n",
    "```\n",
    "and \n",
    "```c\n",
    "if((myarray->m > 1) && (myarray->n > 1)) {\n",
    "    ...\n",
    "}\n",
    "```\n",
    "would be equivalent to \n",
    "```c\n",
    "if(myarray->ndim == 2) {\n",
    "    ...\n",
    "}\n",
    "```\n",
    "\n",
    "One could also save the extra function call `mp_binary_get_size('@', data->typecode, NULL)`, if `itemsize` is available. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Returning and accepting raw bytes\n",
    "\n",
    "It might make sense to have a function that returns the raw content of the `ndarray`. The rationale for this is that this would make direct use of calculation results a piece of cake. E.g., the DAC could be fed as \n",
    "\n",
    "```python\n",
    "length = 100\n",
    "amp = 127\n",
    "\n",
    "x = linspace(0, 2*pi, length)\n",
    "y = ndarray(128 + amp*sin(x), dtype=uint8)\n",
    "buf = y.bytearray()\n",
    "\n",
    "dac = DAC(1)\n",
    "dac.write_timed(buf, 400*length, mode=DAC.CIRCULAR)\n",
    "```\n",
    "\n",
    "Likewise, having the option of writing raw data directly into the `ndarray` could simplify data analysis. E.g., ADC results could be processed as follows:\n",
    "\n",
    "```python\n",
    "length = 100\n",
    "y = ndarray([0]*length, dtype=uint16)\n",
    "\n",
    "adc = pyb.ADC(pyb.Pin.board.X19)\n",
    "tim = pyb.Timer(6, freq=10)\n",
    "buf = y.bytearray()\n",
    "adc.read_timed(buf, tim)\n",
    "\n",
    "y.reshape((10, 10)) # or whatever\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Exposed functions and properties"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "Most of the functions in ndarray.c are internal (i.e., not exposed to the python interpreter). Exception to this rule are the `shape`, `size`, and `rawsize` functions, and the `.unary_op`, `.binary_op`, and `.iter_next` class methods. Note that `rawsize` is is not standard in numpy, and is meant to return the total number of bytes used by the container. Since the RAM of a microcontroller is limited, I deemed this to be a reasonable addition for optimisation purposes, but could later be removed, if it turns out to be of no interest.\n",
    "\n",
    "As mentioned above, `numpy` defines a number of extra members in its `ndarray`. It would be great, if we could return these members as properties of the `ndarray` instance. At the moment, `shape` is a function, as is `rawsize`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Initialisation\n",
    "\n",
    "An `ndarray` can be initialised by passing an iterable (linear arrays), or an iterable of iterables (matrices) into the constructor. In addition, the constructor can take a keyword argument, `dtype`, that will force type conversion. The default value is `float`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:41:06.534510Z",
     "start_time": "2019-09-18T19:41:06.517698Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([1.0, 2.0, 3.0, 4.0], dtype=float)\n",
      "\n",
      " ndarray([[1.0, 2.0, 3.0, 4.0],\n",
      "\t [2.0, 3.0, 4.0, 5.0]], dtype=float)\n",
      "\n",
      " ndarray([[0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0],\n",
      "\t [0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "a = ndarray([1, 2, 3, 4])\n",
    "print(a)\n",
    "a = ndarray([[1, 2, 3, 4], [2, 3, 4, 5]])\n",
    "print('\\n', a)\n",
    "a = ndarray([range(10), range(10)])\n",
    "print('\\n', a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Slicing and subscriptions\n",
    "\n",
    "Subscriptions must resolve the following cases:\n",
    "\n",
    "1. The index is a single item (can be a slice, a list, or an integer). This case must be evaluated properly, even if the array is two-dimensional.\n",
    "2. The index is a tuple: a tuple can contain integers, slices, and Boolean lists. \n",
    "\n",
    "In order to simplify the code, scalars will be turned into slices of length 1. \n",
    "\n",
    "Now, if the index is a scalar, we might have to distinguish two cases: once the array can be a row vector, in which case the method must return a single item as an `mp_obj_t` object. On the other hand, if the array is two-dimensional, the method must return a row as an ndarray. numpy returns an array, if the index was a slice with length one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:04:14.477628Z",
     "start_time": "2019-10-29T07:04:14.468716Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[1]\n"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4])\n",
    "# this is a python object\n",
    "print(a[0])\n",
    "\n",
    "# this is an array\n",
    "print(a[:1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:40:33.906827Z",
     "start_time": "2019-09-18T19:40:33.893889Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2D array: \n",
      " ndarray([[1.0, 2.0, 3.0, 4.0],\n",
      "\t [6.0, 7.0, 8.0, 9.0]], dtype=float)\n",
      "second row of matrix:  ndarray([6.0, 7.0, 8.0, 9.0], dtype=float)\n",
      "\n",
      "1D array:  ndarray([1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float)\n",
      "slize between 1, and 5:  ndarray([2.0, 3.0, 4.0, 5.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "# initialise a matrix\n",
    "a = ndarray([[1, 2, 3, 4], [6, 7, 8, 9]])\n",
    "print('2D array: \\n', a)\n",
    "\n",
    "# print out the second row\n",
    "print('second row of matrix: ', a[1])\n",
    "\n",
    "#initialise an array\n",
    "a = ndarray([1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
    "print('\\n1D array: ', a)\n",
    "# slice the array\n",
    "print('slize between 1, and 5: ', a[1:5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Iterators\n",
    "\n",
    "`ndarray` objects can be iterated on, and just as in numpy, matrices are iterated along their first axis, and they return  `ndarray`s. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:40:23.301446Z",
     "start_time": "2019-09-18T19:40:23.287803Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2D array: \n",
      " ndarray([[1.0, 2.0, 3.0, 4.0],\n",
      "\t [6.0, 7.0, 8.0, 9.0]], dtype=float)\n",
      "\n",
      "row 0:  ndarray([1.0, 2.0, 3.0, 4.0], dtype=float)\n",
      "\n",
      "row 1:  ndarray([6.0, 7.0, 8.0, 9.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "#  initialise a matrix\n",
    "a = ndarray([[1, 2, 3, 4], [6, 7, 8, 9]])\n",
    "print('2D array: \\n', a)\n",
    "\n",
    "# print out the matrix' rows, one by one\n",
    "for i, _a in enumerate(a): \n",
    "    print('\\nrow %d: '%i, _a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "On the other hand, flat arrays return their elements:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T18:37:47.623688Z",
     "start_time": "2019-09-18T18:37:47.604053Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([0, 1, 2, ..., 7, 8, 9], dtype=uint8)\n",
      "element 0:  0\n",
      "element 1:  1\n",
      "element 2:  2\n",
      "element 3:  3\n",
      "element 4:  4\n",
      "element 5:  5\n",
      "element 6:  6\n",
      "element 7:  7\n",
      "element 8:  8\n",
      "element 9:  9\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray, uint8\n",
    "\n",
    "# initialise an array\n",
    "a = ndarray(range(10), dtype=uint8)\n",
    "print('1D array: ', a)\n",
    "\n",
    "# print out the array's elements, one by one\n",
    "for i, _a in enumerate(a): \n",
    "    print('element %d: '%i, _a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Upcasting\n",
    "\n",
    "The following section shows the upcasting rules of `numpy`, and immediately after each case, the test for `ulab`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### uint8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:17:56.244174Z",
     "start_time": "2019-09-27T13:17:56.238095Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=uint8),\n",
       " array([255], dtype=uint8),\n",
       " array([116], dtype=uint8),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint8)\n",
    "b = array([101], dtype=uint8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 727,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T05:05:23.895030Z",
     "start_time": "2019-10-07T05:05:23.864496Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=uint8)\n",
      "ndarray([255], dtype=uint8)\n",
      "ndarray([116], dtype=uint8)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint8)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:18:20.119010Z",
     "start_time": "2019-09-27T13:18:20.107996Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint8)\n",
    "b = array([101], dtype=int8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 728,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T05:05:30.578395Z",
     "start_time": "2019-10-07T05:05:30.552266Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint8)\n",
    "b = ulab.ndarray([101], dtype=ulab.int8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:22:08.632383Z",
     "start_time": "2019-09-27T13:22:08.597893Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=uint16),\n",
       " array([65535], dtype=uint16),\n",
       " array([10100], dtype=uint16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint8)\n",
    "b = array([101], dtype=uint16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 639,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:45:18.275649Z",
     "start_time": "2019-10-05T12:45:18.257651Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=uint16)\n",
      "ndarray([65535], dtype=uint16)\n",
      "ndarray([10100], dtype=uint16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint8)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:24:37.624037Z",
     "start_time": "2019-09-27T13:24:37.607690Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint8)\n",
    "b = array([101], dtype=int16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 638,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:45:11.214800Z",
     "start_time": "2019-10-05T12:45:11.199444Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint8)\n",
    "b = ulab.ndarray([101], dtype=ulab.int16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:27:37.687413Z",
     "start_time": "2019-09-27T13:27:37.679828Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201.]), array([-1.]), array([10100.]), array([0.99009901]))"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint8)\n",
    "b = array([101], dtype=float)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:27:28.418430Z",
     "start_time": "2019-09-27T13:27:28.404415Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201.0], dtype=float)\n",
      "ndarray([-1.0], dtype=float)\n",
      "ndarray([10100.0], dtype=float)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint8)\n",
    "b = ulab.ndarray([101], dtype=ulab.float)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### int8"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:17:04.067672Z",
     "start_time": "2019-09-27T13:17:04.060377Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int8)\n",
    "b = array([101], dtype=uint8)\n",
    "a + b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 637,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:44:59.880670Z",
     "start_time": "2019-10-05T12:44:59.864720Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int8)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:28:35.098056Z",
     "start_time": "2019-09-27T13:28:35.089292Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 44, -54], dtype=int8),\n",
       " array([-100,    0], dtype=int8),\n",
       " array([ 32, -39], dtype=int8),\n",
       " array([-1.78571429,  1.        ]))"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100, 101], dtype=int8)\n",
    "b = array([200, 101], dtype=int8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 636,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:44:48.796807Z",
     "start_time": "2019-10-05T12:44:48.782446Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([44, -54], dtype=int8)\n",
      "ndarray([-100, 0], dtype=int8)\n",
      "ndarray([32, -39], dtype=int8)\n",
      "ndarray([-1.785714268684387, 1.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100, 101], dtype=ulab.int8)\n",
    "b = ulab.ndarray([200, 101], dtype=ulab.int8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:29:05.861558Z",
     "start_time": "2019-09-27T13:29:05.833582Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([300], dtype=int32),\n",
       " array([-100], dtype=int32),\n",
       " array([20000], dtype=int32),\n",
       " array([0.5]))"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int8)\n",
    "b = array([200], dtype=uint16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:28:51.836829Z",
     "start_time": "2019-09-27T13:28:51.824297Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([300], dtype=int16)\n",
      "ndarray([-100], dtype=int16)\n",
      "ndarray([20000], dtype=int16)\n",
      "ndarray([0], dtype=int16)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int8)\n",
    "b = ulab.ndarray([200], dtype=ulab.uint16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:29:26.260592Z",
     "start_time": "2019-09-27T13:29:26.250008Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([300], dtype=int16),\n",
       " array([-100], dtype=int16),\n",
       " array([20000], dtype=int16),\n",
       " array([0.5]))"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int8)\n",
    "b = array([200], dtype=int16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 635,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:44:37.778464Z",
     "start_time": "2019-10-05T12:44:37.762634Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([300], dtype=int16)\n",
      "ndarray([-100], dtype=int16)\n",
      "ndarray([20000], dtype=int16)\n",
      "ndarray([0.5], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int8)\n",
    "b = ulab.ndarray([200], dtype=ulab.int16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:31:05.985497Z",
     "start_time": "2019-09-27T13:31:05.978916Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([300., 202.]),\n",
       " array([-100.,    0.]),\n",
       " array([20000., 10201.]),\n",
       " array([0.5, 1. ]))"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100, 101], dtype=int8)\n",
    "b = array([200, 101], dtype=float)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:30:57.329923Z",
     "start_time": "2019-09-27T13:30:57.317841Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([300.0, 202.0], dtype=float)\n",
      "ndarray([-100.0, 0.0], dtype=float)\n",
      "ndarray([20000.0, 10201.0], dtype=float)\n",
      "ndarray([0.5, 1.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100, 101], dtype=ulab.int8)\n",
    "b = ulab.ndarray([200, 101], dtype=ulab.float)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### uint16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:32:00.347130Z",
     "start_time": "2019-09-27T13:32:00.335940Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([300, 202], dtype=uint16),\n",
       " array([65436,     0], dtype=uint16),\n",
       " array([20000, 10201], dtype=uint16),\n",
       " array([0.5, 1. ]))"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100, 101], dtype=uint16)\n",
    "b = array([200, 101], dtype=uint8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 634,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:44:20.706988Z",
     "start_time": "2019-10-05T12:44:20.691237Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([300, 202], dtype=uint16)\n",
      "ndarray([65436, 0], dtype=uint16)\n",
      "ndarray([20000, 10201], dtype=uint16)\n",
      "ndarray([0.5, 1.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100, 101], dtype=ulab.uint16)\n",
    "b = ulab.ndarray([200, 101], dtype=ulab.uint8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:32:50.000106Z",
     "start_time": "2019-09-27T13:32:49.991721Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 44, 202], dtype=int32),\n",
       " array([156,   0], dtype=int32),\n",
       " array([-5600, 10201], dtype=int32),\n",
       " array([-1.78571429,  1.        ]))"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100, 101], dtype=uint16)\n",
    "b = array([200, 101], dtype=int8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "**This deviates from numpy's behaviour, because we don't have int32.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 633,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:43:42.189060Z",
     "start_time": "2019-10-05T12:43:42.169297Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([44, 202], dtype=uint16)\n",
      "ndarray([156, 0], dtype=uint16)\n",
      "ndarray([59936, 10201], dtype=uint16)\n",
      "ndarray([-1.785714268684387, 1.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100, 101], dtype=ulab.uint16)\n",
    "b = ulab.ndarray([200, 101], dtype=ulab.int8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:03.861582Z",
     "start_time": "2019-09-27T13:33:03.855822Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=uint16),\n",
       " array([65535], dtype=uint16),\n",
       " array([10100], dtype=uint16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint16)\n",
    "b = array([101], dtype=uint16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 632,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:43:34.626766Z",
     "start_time": "2019-10-05T12:43:34.609587Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=uint16)\n",
      "ndarray([65535], dtype=uint16)\n",
      "ndarray([10100], dtype=uint16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint16)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:07.243806Z",
     "start_time": "2019-09-27T13:33:07.238638Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int32),\n",
       " array([-1], dtype=int32),\n",
       " array([10100], dtype=int32),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint16)\n",
    "b = array([101], dtype=int16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "**Again, in numpy, the result is an int32**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 631,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:11:41.675061Z",
     "start_time": "2019-10-05T12:11:41.657376Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201.0], dtype=float)\n",
      "ndarray([-1.0], dtype=float)\n",
      "ndarray([10100.0], dtype=float)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint16)\n",
    "b = ulab.ndarray([101], dtype=ulab.int16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:14.573713Z",
     "start_time": "2019-09-27T13:33:14.567393Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201.]), array([-1.]), array([10100.]), array([0.99009901]))"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=uint16)\n",
    "b = array([101], dtype=float)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:36:32.513195Z",
     "start_time": "2019-09-27T13:36:32.501103Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201.0], dtype=float)\n",
      "ndarray([-1.0], dtype=float)\n",
      "ndarray([10100.0], dtype=float)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.uint16)\n",
    "b = ulab.ndarray([101], dtype=ulab.float)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### int16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:20.954507Z",
     "start_time": "2019-09-27T13:33:20.942470Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int16)\n",
    "b = array([101], dtype=uint8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 630,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:11:28.126990Z",
     "start_time": "2019-10-05T12:11:28.108284Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int16)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:27.072829Z",
     "start_time": "2019-09-27T13:33:27.067428Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int16)\n",
    "b = array([101], dtype=int8)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 629,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T12:11:13.917381Z",
     "start_time": "2019-10-05T12:11:13.898911Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int16)\n",
    "b = ulab.ndarray([101], dtype=ulab.int8)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:33.240290Z",
     "start_time": "2019-09-27T13:33:33.230477Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int32),\n",
       " array([-1], dtype=int32),\n",
       " array([10100], dtype=int32),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int16)\n",
    "b = array([101], dtype=uint16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "**While the results are correct, here we have float instead of int32**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:38:18.902310Z",
     "start_time": "2019-09-27T13:38:18.888977Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201.0], dtype=float)\n",
      "ndarray([-1.0], dtype=float)\n",
      "ndarray([10100.0], dtype=float)\n",
      "ndarray([0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int16)\n",
    "b = ulab.ndarray([101], dtype=ulab.uint16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:38.856727Z",
     "start_time": "2019-09-27T13:33:38.849253Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201], dtype=int16),\n",
       " array([-1], dtype=int16),\n",
       " array([10100], dtype=int16),\n",
       " array([0.99009901]))"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int16)\n",
    "b = array([101], dtype=int16)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:38:52.861603Z",
     "start_time": "2019-09-27T13:38:52.847250Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201], dtype=int16)\n",
      "ndarray([-1], dtype=int16)\n",
      "ndarray([10100], dtype=int16)\n",
      "ndarray([0], dtype=int16)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int16)\n",
    "b = ulab.ndarray([101], dtype=ulab.int16)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:33:45.557516Z",
     "start_time": "2019-09-27T13:33:45.548558Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([201.]), array([-1.]), array([10100.]), array([0.99009901]))"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([100], dtype=int16)\n",
    "b = array([101], dtype=float)\n",
    "a+b, a-b, a*b, a/b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-27T13:39:04.623064Z",
     "start_time": "2019-09-27T13:39:04.613286Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([201.0], dtype=float)\n",
      "ndarray([-1.0], dtype=float)\n",
      "ndarray([10100.0], dtype=float)\n",
      "ndarray([0.9900990128517151], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([100], dtype=ulab.int16)\n",
    "b = ulab.ndarray([101], dtype=ulab.float)\n",
    "print(a+b)\n",
    "print(a-b)\n",
    "print(a*b)\n",
    "print(a/b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "When in an operation the `dtype` of two arrays is different, the result's `dtype` will be decided by the following upcasting rules: \n",
    "\n",
    "1. Operations with two `ndarray`s of the same `dtype` preserve their `dtype`, even when the results overflow.\n",
    "\n",
    "2. if either of the operands is a float, the results is also a float\n",
    "\n",
    "3. \n",
    "    - `uint8` + `int8` => `int16`, \n",
    "    - `uint8` + `int16` => `int16`\n",
    "    - `uint8` + `uint16` => `uint16`\n",
    "    \n",
    "    - `int8` + `int16` => `int16`\n",
    "    - `int8` + `uint16` => `uint16` (in numpy, it is a `int32`)\n",
    "\n",
    "    - `uint16` + `int16` => `float` (in numpy, it is a `int32`)\n",
    "    \n",
    "4. When the right hand side of a binary operator is a micropython variable, `mp_obj_int`, or `mp_obj_float`, then the result will be promoted to `dtype` `float`. This is necessary, because a micropython integer can be 31 bites wide.\n",
    "\n",
    "`numpy` is also inconsistent in how it represents `dtype`: as an argument, it is denoted by the constants `int8`, `uint8`, etc., while a string will be returned, if the user asks for the type of an array.\n",
    "\n",
    "The upcasting rules are stipulated in a single C function, `upcasting()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### upcasting rules with scalars\n",
    "\n",
    "When a "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-03T17:18:04.801711Z",
     "start_time": "2019-10-03T17:18:04.794511Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(dtype('int16'),\n",
       " array([ 555, 1110, 1665], dtype=int16),\n",
       " dtype('int8'),\n",
       " array([-43, -86, 127], dtype=int8))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3], dtype=int8)\n",
    "b = a * 555\n",
    "a *= -555\n",
    "b.dtype, b, a.dtype, a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Binary operations\n",
    "\n",
    "In the standard binary operations, the operands are either two `ndarray`s or an `ndarray`, and a number. From the C standpoint, these operations are probably the most difficult: the problem is that two operands, each with 5 possible C types are added, multiplied, subtracted, or divided, hence making the number of possible combinations large. In order to mitigate the situation, we make use of macros: this would make most of the code type-agnostic. \n",
    "\n",
    "Also, when an operation involves a scalar, and an `ndarray`, we will turn the scalar into an `ndarray` of length 1. In this way, we can reduce the code size of the binary handler by almost a factor of two."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 758,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T17:06:49.761517Z",
     "start_time": "2019-10-07T17:06:49.738828Z"
    },
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([2.0, 4.0, 6.0], dtype=float)\n",
      "ndarray([5.0, 10.0, 15.0], dtype=float)\n",
      "ndarray([0.5, 1.0, 1.5], dtype=float)\n",
      "ndarray([-9.0, -8.0, -7.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray, float\n",
    "\n",
    "a = ndarray([1, 2, 3], dtype=float)\n",
    "print(a + a)\n",
    "print(a * 5.0)\n",
    "print(a / 2)\n",
    "print(a - 10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### in-place operators\n",
    "\n",
    "In-place operators preserve the type of the array's type. Here are a couple of caveats:\n",
    "\n",
    "1. overflow obviously occurs\n",
    "2. float can be added only to float type\n",
    "3. true divide fails, except when the array is of type float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 397,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:56:43.992475Z",
     "start_time": "2019-10-05T05:56:43.985525Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([221, 222, 223,   4], dtype=uint8)"
      ]
     },
     "execution_count": 397,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=uint8)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 400,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:58:00.203706Z",
     "start_time": "2019-10-05T05:58:00.193454Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-35, -34, -33,   4], dtype=int8)"
      ]
     },
     "execution_count": 400,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=int8)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 403,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:58:27.581230Z",
     "start_time": "2019-10-05T05:58:27.571038Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([221, 222, 223, 260], dtype=uint16)"
      ]
     },
     "execution_count": 403,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=uint16)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 404,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:58:37.600710Z",
     "start_time": "2019-10-05T05:58:37.592757Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([221, 222, 223, 260], dtype=int16)"
      ]
     },
     "execution_count": 404,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=int16)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 405,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:58:46.439586Z",
     "start_time": "2019-10-05T05:58:46.429884Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([221., 222., 223., 260.])"
      ]
     },
     "execution_count": 405,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=float)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 406,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:58:59.188248Z",
     "start_time": "2019-10-05T05:58:59.170331Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-406-658f10292bb1>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m40\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m220.0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: Cannot cast ufunc add output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=uint8)\n",
    "a += 220.0\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 407,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T05:59:50.071230Z",
     "start_time": "2019-10-05T05:59:50.062205Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "No loop matching the specified signature and casting\nwas found for ufunc true_divide",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-407-1ed2746d8aeb>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m40\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0muint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0;36m22\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      3\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: No loop matching the specified signature and casting\nwas found for ufunc true_divide"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=uint8)\n",
    "a /= 22\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 408,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:00:13.956065Z",
     "start_time": "2019-10-05T06:00:13.946612Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([221., 222., 223., 260.])"
      ]
     },
     "execution_count": 408,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 40], dtype=float)\n",
    "a += 220\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 413,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:05:11.019007Z",
     "start_time": "2019-10-05T06:05:11.004555Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'b') according to the casting rule ''same_kind''",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-413-80e572931886>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: ufunc 'true_divide' output (typecode 'd') could not be coerced to provided output parameter (typecode 'b') according to the casting rule ''same_kind''"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4], dtype=int8)\n",
    "b = array([5, 10, 15, 20], dtype=float)\n",
    "a /= b\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 414,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:06:02.920934Z",
     "start_time": "2019-10-05T06:06:02.910488Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "No loop matching the specified signature and casting\nwas found for ufunc true_divide",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-414-a76603b93818>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0mb\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0marray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m15\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mint8\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m/=\u001b[0m \u001b[0mb\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: No loop matching the specified signature and casting\nwas found for ufunc true_divide"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4], dtype=int8)\n",
    "b = array([5, 10, 15, 20], dtype=int8)\n",
    "a /= b\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:07:50.122999Z",
     "start_time": "2019-10-05T06:07:50.116970Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([   5,   20,   45, -112], dtype=int8)"
      ]
     },
     "execution_count": 419,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4], dtype=int8)\n",
    "b = array([5, 10, 15, 100], dtype=int16)\n",
    "a *= b\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 424,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:10:33.186832Z",
     "start_time": "2019-10-05T06:10:33.179206Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  4,  9, 16], dtype=int8)"
      ]
     },
     "execution_count": 424,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4], dtype=int8)\n",
    "a **= 2\n",
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Comparison operators\n",
    "\n",
    "These return list(s) of Booleans."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 762,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T17:09:24.449754Z",
     "start_time": "2019-10-07T17:09:24.426063Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[True, True, True, False, False, False, False, False]\n",
      "[True, True, True, True, False, False, False, False]\n",
      "[False, False, False, False, True, True, True, True]\n",
      "[False, False, False, True, True, True, True, True]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([1, 2, 3, 4, 5, 6, 7, 8])\n",
    "a.reshape((1, 8))\n",
    "print(a < 4)\n",
    "print(a <= 4)\n",
    "print(a > 4)\n",
    "print(a >= 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 763,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T17:09:28.675848Z",
     "start_time": "2019-10-07T17:09:28.654105Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[True, True, True, False], [False, False, False, False]]\n",
      "[[True, True, True, True], [False, False, False, False]]\n",
      "[[False, False, False, False], [True, True, True, True]]\n",
      "[[False, False, False, True], [True, True, True, True]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "print(a < 4)\n",
    "print(a <= 4)\n",
    "print(a > 4)\n",
    "print(a >= 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 764,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T17:09:34.572107Z",
     "start_time": "2019-10-07T17:09:34.551796Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[True, True, True, True, False, False, False, False]\n",
      "[True, True, True, True, True, False, False, False]\n",
      "[False, False, False, False, False, True, True, True]\n",
      "[False, False, False, False, True, True, True, True]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([1, 2, 3, 4, 4, 6, 7, 8])\n",
    "a.transpose()\n",
    "b = ulab.ndarray([8, 7, 6, 5, 4, 3, 2, 1])\n",
    "b.transpose()\n",
    "print(a < b)\n",
    "print(a <= b)\n",
    "print(a > b)\n",
    "print(a >= b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 765,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T17:09:43.620586Z",
     "start_time": "2019-10-07T17:09:43.601075Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[True, True, False, False], [False, False, False, False]]\n",
      "[[True, True, False, False], [False, False, False, False]]\n",
      "[[False, False, True, True], [True, True, True, True]]\n",
      "[[False, False, True, True], [True, True, True, True]]\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "b = ulab.ndarray([[8, 7, 1, 1], [4, 3, 2, 1]], dtype=ulab.int8)\n",
    "print(a < b)\n",
    "print(a <= b)\n",
    "print(a > b)\n",
    "print(a >= b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Simple running weighted average\n",
    "\n",
    "With the subscription tools, a weighted running average can very easily be implemented as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T18:41:48.382588Z",
     "start_time": "2019-09-18T18:41:48.366657Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "3.6\n",
      "4.8\n",
      "5.6\n",
      "6.0\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray, mean, roll\n",
    "\n",
    "# These are the weights; the last entry is the most dominant\n",
    "weight = ndarray([1, 2, 3, 4, 5]) \n",
    "\n",
    "# initial array of samples\n",
    "samples = ndarray([0]*5)\n",
    "\n",
    "for i in range(5):\n",
    "    # a new datum is inserted on the right hand side. This simply overwrites whatever was in the last slot\n",
    "    samples[-1] = 2\n",
    "    print(mean(samples*weight))\n",
    "    # the data are shifted by one position to the left\n",
    "    roll(samples, 1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Unary operators\n",
    "\n",
    "At the moment, only `len` is implemented, which returns the number of elements for one-dimensional arrays, and the length of the first axis for matrices. One should consider other possibilities."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T18:43:16.700131Z",
     "start_time": "2019-09-18T18:43:16.685155Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0], dtype=float)\n",
      "length of array:  10\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "# initialise an array\n",
    "a = ndarray(range(10))\n",
    "print('1D array: ', a)\n",
    "\n",
    "# print out the array's length\n",
    "print('length of array: ', len(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:39:59.987378Z",
     "start_time": "2019-09-18T19:39:59.970763Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2D array: \n",
      " ndarray([[0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0],\n",
      "\t [0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0],\n",
      "\t [0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0]], dtype=float)\n",
      "length of array:  3\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "# initialise a matrix\n",
    "a = ndarray([range(10), range(10), range(10)])\n",
    "print('2D array: \\n', a)\n",
    "\n",
    "# print out the array's elements, one by one\n",
    "print('length of array: ', len(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Class methods: shape, size, rawsize, flatten"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 221,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-04T16:11:23.387157Z",
     "start_time": "2019-10-04T16:11:23.373856Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0], dtype=float)\n",
      "shape:  (10, 1)\n",
      "size 0:  10 \n",
      "size 1:  10 \n",
      "size 2:  1\n",
      "raw size:  (10, 1, 40, 10, 4)\n",
      "\n",
      "2D array: \n",
      " ndarray([[0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0],\n",
      "\t [0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0],\n",
      "\t [0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0]], dtype=float)\n",
      "shape:  (3, 10)\n",
      "size 0:  30 \n",
      "size 1:  3 \n",
      "size 2:  10\n",
      "raw size:  (3, 10, 120, 30, 4)\n",
      "\n",
      "2D array: \n",
      " ndarray([[0.0, 1.0, 2.0],\n",
      "\t [0.0, 1.0, 2.0],\n",
      "\t [0.0, 1.0, 2.0]], dtype=float)\n",
      "flattened array: (C) ndarray([0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0], dtype=float)\n",
      "flattened array: (F) ndarray([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "# initialise an array\n",
    "a = ndarray(range(10))\n",
    "print('1D array: ', a)\n",
    "\n",
    "# print out the shape\n",
    "print('shape: ', a.shape())\n",
    "\n",
    "#print out the size\n",
    "print('size 0: ', a.size(0), '\\nsize 1: ', a.size(1), '\\nsize 2: ', a.size(2))\n",
    "\n",
    "#print out the raw size\n",
    "print('raw size: ', a.rawsize())\n",
    "\n",
    "# initialise a matrix\n",
    "a = ndarray([range(10), range(10), range(10)])\n",
    "print('\\n2D array: \\n', a)\n",
    "\n",
    "# print out the shape\n",
    "print('shape: ', a.shape())\n",
    "\n",
    "#print out the size\n",
    "print('size 0: ', a.size(0), '\\nsize 1: ', a.size(1), '\\nsize 2: ', a.size(2))\n",
    "\n",
    "#print out the raw size\n",
    "print('raw size: ', a.rawsize())\n",
    "\n",
    "#flattened array\n",
    "a = ndarray([range(3), range(3), range(3)])\n",
    "print('\\n2D array: \\n', a)\n",
    "print('flattened array: (C)', a.flatten(order='C'))\n",
    "print('flattened array: (F)', a.flatten(order='F'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarray.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 373,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-01T13:05:40.505260Z",
     "start_time": "2019-11-01T13:05:40.499409Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 4891 bytes to ndarray.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode ndarray.h\n",
    "\n",
    "#ifndef _NDARRAY_\n",
    "#define _NDARRAY_\n",
    "\n",
    "#include \"py/objarray.h\"\n",
    "#include \"py/binary.h\"\n",
    "#include \"py/objstr.h\"\n",
    "#include \"py/objlist.h\"\n",
    "\n",
    "#define PRINT_MAX  10\n",
    "\n",
    "#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT\n",
    "#define FLOAT_TYPECODE 'f'\n",
    "#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE\n",
    "#define FLOAT_TYPECODE 'd'\n",
    "#endif\n",
    "\n",
    "const mp_obj_type_t ulab_ndarray_type;\n",
    "\n",
    "enum NDARRAY_TYPE {\n",
    "    NDARRAY_UINT8 = 'B',\n",
    "    NDARRAY_INT8 = 'b',\n",
    "    NDARRAY_UINT16 = 'H', \n",
    "    NDARRAY_INT16 = 'h',\n",
    "    NDARRAY_FLOAT = FLOAT_TYPECODE,\n",
    "};\n",
    "\n",
    "typedef struct _ndarray_obj_t {\n",
    "    mp_obj_base_t base;\n",
    "    size_t m, n;\n",
    "    size_t len;\n",
    "    mp_obj_array_t *array;\n",
    "    size_t bytes;\n",
    "} ndarray_obj_t;\n",
    "\n",
    "mp_obj_t mp_obj_new_ndarray_iterator(mp_obj_t , size_t , mp_obj_iter_buf_t *);\n",
    "\n",
    "mp_float_t ndarray_get_float_value(void *, uint8_t , size_t );\n",
    "void fill_array_iterable(mp_float_t *, mp_obj_t );\n",
    "\n",
    "void ndarray_print_row(const mp_print_t *, mp_obj_array_t *, size_t , size_t );\n",
    "void ndarray_print(const mp_print_t *, mp_obj_t , mp_print_kind_t );\n",
    "void ndarray_assign_elements(mp_obj_array_t *, mp_obj_t , uint8_t , size_t *);\n",
    "ndarray_obj_t *create_new_ndarray(size_t , size_t , uint8_t );\n",
    "\n",
    "mp_obj_t ndarray_copy(mp_obj_t );\n",
    "mp_obj_t ndarray_make_new(const mp_obj_type_t *, size_t , size_t , const mp_obj_t *);\n",
    "mp_obj_t ndarray_subscr(mp_obj_t , mp_obj_t , mp_obj_t );\n",
    "mp_obj_t ndarray_getiter(mp_obj_t , mp_obj_iter_buf_t *);\n",
    "mp_obj_t ndarray_binary_op(mp_binary_op_t , mp_obj_t , mp_obj_t );\n",
    "mp_obj_t ndarray_unary_op(mp_unary_op_t , mp_obj_t );\n",
    "\n",
    "mp_obj_t ndarray_shape(mp_obj_t );\n",
    "mp_obj_t ndarray_rawsize(mp_obj_t );\n",
    "mp_obj_t ndarray_flatten(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t ndarray_asbytearray(mp_obj_t );\n",
    "\n",
    "#define CREATE_SINGLE_ITEM(outarray, type, typecode, value) do {\\\n",
    "    ndarray_obj_t *tmp = create_new_ndarray(1, 1, (typecode));\\\n",
    "    type *tmparr = (type *)tmp->array->items;\\\n",
    "    tmparr[0] = (type)(value);\\\n",
    "    (outarray) = MP_OBJ_FROM_PTR(tmp);\\\n",
    "} while(0)\n",
    "\n",
    "/*  \n",
    "    mp_obj_t row = mp_obj_new_list(n, NULL);\n",
    "    mp_obj_list_t *row_ptr = MP_OBJ_TO_PTR(row);\n",
    "    \n",
    "    should work outside the loop, but it doesn't. Go figure! \n",
    "*/\n",
    "\n",
    "#define RUN_BINARY_LOOP(typecode, type_out, type_left, type_right, ol, or, op) do {\\\n",
    "    type_left *left = (type_left *)(ol)->array->items;\\\n",
    "    type_right *right = (type_right *)(or)->array->items;\\\n",
    "    uint8_t inc = 0;\\\n",
    "    if((or)->array->len > 1) inc = 1;\\\n",
    "    if(((op) == MP_BINARY_OP_ADD) || ((op) == MP_BINARY_OP_SUBTRACT) || ((op) == MP_BINARY_OP_MULTIPLY)) {\\\n",
    "        ndarray_obj_t *out = create_new_ndarray(ol->m, ol->n, typecode);\\\n",
    "        type_out *(odata) = (type_out *)out->array->items;\\\n",
    "        if((op) == MP_BINARY_OP_ADD) { for(size_t i=0, j=0; i < (ol)->array->len; i++, j+=inc) odata[i] = left[i] + right[j];}\\\n",
    "        if((op) == MP_BINARY_OP_SUBTRACT) { for(size_t i=0, j=0; i < (ol)->array->len; i++, j+=inc) odata[i] = left[i] - right[j];}\\\n",
    "        if((op) == MP_BINARY_OP_MULTIPLY) { for(size_t i=0, j=0; i < (ol)->array->len; i++, j+=inc) odata[i] = left[i] * right[j];}\\\n",
    "        return MP_OBJ_FROM_PTR(out);\\\n",
    "    } else if((op) == MP_BINARY_OP_TRUE_DIVIDE) {\\\n",
    "        ndarray_obj_t *out = create_new_ndarray(ol->m, ol->n, NDARRAY_FLOAT);\\\n",
    "        mp_float_t *odata = (mp_float_t *)out->array->items;\\\n",
    "        for(size_t i=0, j=0; i < (ol)->array->len; i++, j+=inc) odata[i] = (mp_float_t)left[i]/(mp_float_t)right[j];\\\n",
    "        return MP_OBJ_FROM_PTR(out);\\\n",
    "    } else if(((op) == MP_BINARY_OP_LESS) || ((op) == MP_BINARY_OP_LESS_EQUAL) ||  \\\n",
    "             ((op) == MP_BINARY_OP_MORE) || ((op) == MP_BINARY_OP_MORE_EQUAL)) {\\\n",
    "        mp_obj_t out_list = mp_obj_new_list(0, NULL);\\\n",
    "        size_t m = (ol)->m, n = (ol)->n;\\\n",
    "        for(size_t i=0, r=0; i < m; i++, r+=inc) {\\\n",
    "            mp_obj_t row = mp_obj_new_list(n, NULL);\\\n",
    "            mp_obj_list_t *row_ptr = MP_OBJ_TO_PTR(row);\\\n",
    "            for(size_t j=0, s=0; j < n; j++, s+=inc) {\\\n",
    "                row_ptr->items[j] = mp_const_false;\\\n",
    "                if((op) == MP_BINARY_OP_LESS) {\\\n",
    "                    if(left[i*n+j] < right[r*n+s]) row_ptr->items[j] = mp_const_true;\\\n",
    "                } else if((op) == MP_BINARY_OP_LESS_EQUAL) {\\\n",
    "                    if(left[i*n+j] <= right[r*n+s]) row_ptr->items[j] = mp_const_true;\\\n",
    "                } else if((op) == MP_BINARY_OP_MORE) {\\\n",
    "                    if(left[i*n+j] > right[r*n+s]) row_ptr->items[j] = mp_const_true;\\\n",
    "                } else if((op) == MP_BINARY_OP_MORE_EQUAL) {\\\n",
    "                    if(left[i*n+j] >= right[r*n+s]) row_ptr->items[j] = mp_const_true;\\\n",
    "                }\\\n",
    "            }\\\n",
    "            if(m == 1) return row;\\\n",
    "            mp_obj_list_append(out_list, row);\\\n",
    "        }\\\n",
    "        return out_list;\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ndarray.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 632,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-04T18:27:33.495075Z",
     "start_time": "2019-11-04T18:27:33.480012Z"
    },
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 41199 bytes to ndarray.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode ndarray.c\n",
    "\n",
    "#include <math.h>\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include <string.h>\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/binary.h\"\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/objtuple.h\"\n",
    "#include \"ndarray.h\"\n",
    "\n",
    "// This function is copied verbatim from objarray.c\n",
    "STATIC mp_obj_array_t *array_new(char typecode, size_t n) {\n",
    "    int typecode_size = mp_binary_get_size('@', typecode, NULL);\n",
    "    mp_obj_array_t *o = m_new_obj(mp_obj_array_t);\n",
    "    // this step could probably be skipped: we are never going to store a bytearray per se\n",
    "    #if MICROPY_PY_BUILTINS_BYTEARRAY && MICROPY_PY_ARRAY\n",
    "    o->base.type = (typecode == BYTEARRAY_TYPECODE) ? &mp_type_bytearray : &mp_type_array;\n",
    "    #elif MICROPY_PY_BUILTINS_BYTEARRAY\n",
    "    o->base.type = &mp_type_bytearray;\n",
    "    #else\n",
    "    o->base.type = &mp_type_array;\n",
    "    #endif\n",
    "    o->typecode = typecode;\n",
    "    o->free = 0;\n",
    "    o->len = n;\n",
    "    o->items = m_new(byte, typecode_size * o->len);\n",
    "    return o;\n",
    "}\n",
    "\n",
    "mp_float_t ndarray_get_float_value(void *data, uint8_t typecode, size_t index) {\n",
    "    if(typecode == NDARRAY_UINT8) {\n",
    "        return (mp_float_t)((uint8_t *)data)[index];\n",
    "    } else if(typecode == NDARRAY_INT8) {\n",
    "        return (mp_float_t)((int8_t *)data)[index];\n",
    "    } else if(typecode == NDARRAY_UINT16) {\n",
    "        return (mp_float_t)((uint16_t *)data)[index];\n",
    "    } else if(typecode == NDARRAY_INT16) {\n",
    "        return (mp_float_t)((int16_t *)data)[index];\n",
    "    } else {\n",
    "        return (mp_float_t)((mp_float_t *)data)[index];\n",
    "    }\n",
    "}\n",
    "\n",
    "void fill_array_iterable(mp_float_t *array, mp_obj_t iterable) {\n",
    "    mp_obj_iter_buf_t x_buf;\n",
    "    mp_obj_t x_item, x_iterable = mp_getiter(iterable, &x_buf);\n",
    "    size_t i=0;\n",
    "    while ((x_item = mp_iternext(x_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        array[i] = (mp_float_t)mp_obj_get_float(x_item);\n",
    "        i++;\n",
    "    }\n",
    "}\n",
    "\n",
    "void ndarray_print_row(const mp_print_t *print, mp_obj_array_t *data, size_t n0, size_t n) {\n",
    "    mp_print_str(print, \"[\");\n",
    "    size_t i;\n",
    "    if(n < PRINT_MAX) { // if the array is short, print everything\n",
    "        mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0), PRINT_REPR);\n",
    "        for(i=1; i<n; i++) {\n",
    "            mp_print_str(print, \", \");\n",
    "            mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0+i), PRINT_REPR);\n",
    "        }\n",
    "    } else {\n",
    "        mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0), PRINT_REPR);\n",
    "        for(i=1; i<3; i++) {\n",
    "            mp_print_str(print, \", \");\n",
    "            mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0+i), PRINT_REPR);\n",
    "        }\n",
    "        mp_printf(print, \", ..., \");\n",
    "        mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0+n-3), PRINT_REPR);\n",
    "        for(size_t i=1; i<3; i++) {\n",
    "            mp_print_str(print, \", \");\n",
    "            mp_obj_print_helper(print, mp_binary_get_val_array(data->typecode, data->items, n0+n-3+i), PRINT_REPR);\n",
    "        }\n",
    "    }\n",
    "    mp_print_str(print, \"]\");\n",
    "}\n",
    "\n",
    "void ndarray_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {\n",
    "    (void)kind;\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    mp_print_str(print, \"array(\");\n",
    "    \n",
    "    if(self->array->len == 0) {\n",
    "        mp_print_str(print, \"[]\");\n",
    "    } else {\n",
    "        if((self->m == 1) || (self->n == 1)) {\n",
    "            ndarray_print_row(print, self->array, 0, self->array->len);\n",
    "        } else {\n",
    "            // TODO: add vertical ellipses for the case, when self->m > PRINT_MAX\n",
    "            mp_print_str(print, \"[\");\n",
    "            ndarray_print_row(print, self->array, 0, self->n);\n",
    "            for(size_t i=1; i < self->m; i++) {\n",
    "                mp_print_str(print, \",\\n\\t \");\n",
    "                ndarray_print_row(print, self->array, i*self->n, self->n);\n",
    "            }\n",
    "            mp_print_str(print, \"]\");\n",
    "        }\n",
    "    }\n",
    "    if(self->array->typecode == NDARRAY_UINT8) {\n",
    "        mp_print_str(print, \", dtype=uint8)\");\n",
    "    } else if(self->array->typecode == NDARRAY_INT8) {\n",
    "        mp_print_str(print, \", dtype=int8)\");\n",
    "    } else if(self->array->typecode == NDARRAY_UINT16) {\n",
    "        mp_print_str(print, \", dtype=uint16)\");\n",
    "    } else if(self->array->typecode == NDARRAY_INT16) {\n",
    "        mp_print_str(print, \", dtype=int16)\");\n",
    "    } else if(self->array->typecode == NDARRAY_FLOAT) {\n",
    "        mp_print_str(print, \", dtype=float)\");\n",
    "    }\n",
    "}\n",
    "\n",
    "void ndarray_assign_elements(mp_obj_array_t *data, mp_obj_t iterable, uint8_t typecode, size_t *idx) {\n",
    "    // assigns a single row in the matrix\n",
    "    mp_obj_t item;\n",
    "    while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        mp_binary_set_val_array(typecode, data->items, (*idx)++, item);\n",
    "    }\n",
    "}\n",
    "\n",
    "ndarray_obj_t *create_new_ndarray(size_t m, size_t n, uint8_t typecode) {\n",
    "    // Creates the base ndarray with shape (m, n), and initialises the values to straight 0s\n",
    "    ndarray_obj_t *ndarray = m_new_obj(ndarray_obj_t);\n",
    "    ndarray->base.type = &ulab_ndarray_type;\n",
    "    ndarray->m = m;\n",
    "    ndarray->n = n;\n",
    "    mp_obj_array_t *array = array_new(typecode, m*n);\n",
    "    ndarray->bytes = m * n * mp_binary_get_size('@', typecode, NULL);\n",
    "    // this should set all elements to 0, irrespective of the of the typecode (all bits are zero)\n",
    "    // we could, perhaps, leave this step out, and initialise the array only, when needed\n",
    "    memset(array->items, 0, ndarray->bytes); \n",
    "    ndarray->array = array;\n",
    "    return ndarray;\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_copy(mp_obj_t self_in) {\n",
    "    // returns a verbatim (shape and typecode) copy of self_in\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    ndarray_obj_t *out = create_new_ndarray(self->m, self->n, self->array->typecode);\n",
    "    memcpy(out->array->items, self->array->items, self->bytes);\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "}\n",
    "\n",
    "STATIC uint8_t ndarray_init_helper(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },\n",
    "        { MP_QSTR_dtype, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = NDARRAY_FLOAT } },\n",
    "    };\n",
    "    \n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    uint8_t dtype = args[1].u_int;\n",
    "    return dtype;\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {\n",
    "    mp_arg_check_num(n_args, n_kw, 1, 2, true);\n",
    "    mp_map_t kw_args;\n",
    "    mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);\n",
    "    uint8_t dtype = ndarray_init_helper(n_args, args, &kw_args);\n",
    "\n",
    "    size_t len1, len2=0, i=0;\n",
    "    mp_obj_t len_in = mp_obj_len_maybe(args[0]);\n",
    "    if (len_in == MP_OBJ_NULL) {\n",
    "        mp_raise_ValueError(\"first argument must be an iterable\");\n",
    "    } else {\n",
    "        // len1 is either the number of rows (for matrices), or the number of elements (row vectors)\n",
    "        len1 = MP_OBJ_SMALL_INT_VALUE(len_in);\n",
    "    }\n",
    "\n",
    "    // We have to figure out, whether the first element of the iterable is an iterable itself\n",
    "    // Perhaps, there is a more elegant way of handling this\n",
    "    mp_obj_iter_buf_t iter_buf1;\n",
    "    mp_obj_t item1, iterable1 = mp_getiter(args[0], &iter_buf1);\n",
    "    while ((item1 = mp_iternext(iterable1)) != MP_OBJ_STOP_ITERATION) {\n",
    "        len_in = mp_obj_len_maybe(item1);\n",
    "        if(len_in != MP_OBJ_NULL) { // indeed, this seems to be an iterable\n",
    "            // Next, we have to check, whether all elements in the outer loop have the same length\n",
    "            if(i > 0) {\n",
    "                if(len2 != MP_OBJ_SMALL_INT_VALUE(len_in)) {\n",
    "                    mp_raise_ValueError(\"iterables are not of the same length\");\n",
    "                }\n",
    "            }\n",
    "            len2 = MP_OBJ_SMALL_INT_VALUE(len_in);\n",
    "            i++;\n",
    "        }\n",
    "    }\n",
    "    // By this time, it should be established, what the shape is, so we can now create the array\n",
    "    ndarray_obj_t *self = create_new_ndarray((len2 == 0) ? 1 : len1, (len2 == 0) ? len1 : len2, dtype);\n",
    "    iterable1 = mp_getiter(args[0], &iter_buf1);\n",
    "    i = 0;\n",
    "    if(len2 == 0) { // the first argument is a single iterable\n",
    "        ndarray_assign_elements(self->array, iterable1, dtype, &i);\n",
    "    } else {\n",
    "        mp_obj_iter_buf_t iter_buf2;\n",
    "        mp_obj_t iterable2; \n",
    "\n",
    "        while ((item1 = mp_iternext(iterable1)) != MP_OBJ_STOP_ITERATION) {\n",
    "            iterable2 = mp_getiter(item1, &iter_buf2);\n",
    "            ndarray_assign_elements(self->array, iterable2, dtype, &i);\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(self);\n",
    "}\n",
    "\n",
    "size_t slice_length(mp_bound_slice_t slice) {\n",
    "    // TODO: check, whether this is true!\n",
    "    if(slice.step < 0) {\n",
    "        slice.step = -slice.step;\n",
    "        return (slice.start - slice.stop) / slice.step;\n",
    "    } else {\n",
    "        return (slice.stop - slice.start) / slice.step;        \n",
    "    }\n",
    "}\n",
    "\n",
    "size_t true_length(mp_obj_t bool_list) {\n",
    "    // returns the number of Trues in a Boolean list\n",
    "    // I wonder, wouldn't this be faster, if we looped through bool_list->items instead?\n",
    "    mp_obj_iter_buf_t iter_buf;\n",
    "    mp_obj_t item, iterable = mp_getiter(bool_list, &iter_buf);\n",
    "    size_t trues = 0;\n",
    "    while((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        if(!mp_obj_is_type(item, &mp_type_bool)) {\n",
    "            // numpy seems to be a little bit inconsistent in when an index is considered\n",
    "            // to be True/False. Bail out immediately, if the items are not True/False\n",
    "            return 0;\n",
    "        }\n",
    "        if(mp_obj_is_true(item)) {\n",
    "            trues++;\n",
    "        }\n",
    "    }\n",
    "    return trues;\n",
    "}\n",
    "\n",
    "mp_bound_slice_t generate_slice(mp_uint_t n, mp_obj_t index) {\n",
    "    // micropython seems to have difficulties with negative steps\n",
    "    mp_bound_slice_t slice;\n",
    "    if(MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
    "        mp_seq_get_fast_slice_indexes(n, index, &slice);\n",
    "    } else if(mp_obj_is_int(index)) {\n",
    "        int32_t _index = mp_obj_get_int(index);\n",
    "        if(_index < 0) {\n",
    "            _index += n;\n",
    "        } \n",
    "        if((_index >= n) || (_index < 0)) {\n",
    "            mp_raise_msg(&mp_type_IndexError, \"index is out of bounds\");\n",
    "        }\n",
    "        slice.start = _index;\n",
    "        slice.stop = _index + 1;\n",
    "        slice.step = 1;\n",
    "    } else {\n",
    "        mp_raise_msg(&mp_type_IndexError, \"indices must be integers, slices, or Boolean lists\");\n",
    "    }\n",
    "    return slice;\n",
    "}\n",
    "\n",
    "mp_bound_slice_t simple_slice(int16_t start, int16_t stop, int16_t step) {\n",
    "    mp_bound_slice_t slice;\n",
    "    slice.start = start;\n",
    "    slice.stop = stop;\n",
    "    slice.step = step;\n",
    "    return slice;\n",
    "}\n",
    "\n",
    "void insert_binary_value(ndarray_obj_t *ndarray, size_t nd_index, ndarray_obj_t *values, size_t value_index) {\n",
    "    // there is probably a more elegant implementation...\n",
    "    mp_obj_t tmp = mp_binary_get_val_array(values->array->typecode, values->array->items, value_index);\n",
    "    if((values->array->typecode == NDARRAY_FLOAT) && (ndarray->array->typecode != NDARRAY_FLOAT)) {\n",
    "        // workaround: rounding seems not to work in the arm compiler\n",
    "        int32_t x = (int32_t)floorf(mp_obj_get_float(tmp)+0.5);\n",
    "        tmp = mp_obj_new_int(x);\n",
    "    }\n",
    "    mp_binary_set_val_array(ndarray->array->typecode, ndarray->array->items, nd_index, tmp); \n",
    "}\n",
    "\n",
    "mp_obj_t insert_slice_list(ndarray_obj_t *ndarray, size_t m, size_t n, \n",
    "                            mp_bound_slice_t row, mp_bound_slice_t column, \n",
    "                            mp_obj_t row_list, mp_obj_t column_list, \n",
    "                            ndarray_obj_t *values) {\n",
    "    if((m != values->m) && (n != values->n)) {\n",
    "        if((values->array->len != 1)) { // not a single item\n",
    "            mp_raise_ValueError(\"could not broadast input array from shape\");\n",
    "        }\n",
    "    }\n",
    "    size_t cindex, rindex;\n",
    "    // M, and N are used to manipulate how the source index is incremented in the loop\n",
    "    uint8_t M = 1, N = 1;\n",
    "    if(values->m == 1) {\n",
    "        M = 0;\n",
    "    }\n",
    "    if(values->n == 1) {\n",
    "        N = 0;\n",
    "    }\n",
    "    \n",
    "    if(row_list == mp_const_none) { // rows are indexed by a slice\n",
    "        rindex = row.start;\n",
    "        if(column_list == mp_const_none) { // columns are indexed by a slice\n",
    "            for(size_t i=0; i < m; i++) {\n",
    "                cindex = column.start;\n",
    "                for(size_t j=0; j < n; j++) {\n",
    "                    insert_binary_value(ndarray, rindex*ndarray->n+cindex, values, i*M*n+j*N);\n",
    "                    cindex += column.step;\n",
    "                }\n",
    "                rindex += row.step;\n",
    "            }\n",
    "        } else { // columns are indexed by a Boolean list\n",
    "            mp_obj_iter_buf_t column_iter_buf;\n",
    "            mp_obj_t column_item, column_iterable;\n",
    "            for(size_t i=0; i < m; i++) {\n",
    "                column_iterable = mp_getiter(column_list, &column_iter_buf);\n",
    "                size_t j = 0;\n",
    "                cindex = 0;\n",
    "                while((column_item = mp_iternext(column_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                    if(mp_obj_is_true(column_item)) {\n",
    "                        insert_binary_value(ndarray, rindex*ndarray->n+cindex, values, i*M*n+j*N);\n",
    "                        j++;\n",
    "                    }\n",
    "                    cindex++;\n",
    "                }\n",
    "                rindex += row.step;\n",
    "            }\n",
    "        }\n",
    "    } else { // rows are indexed by a Boolean list\n",
    "        mp_obj_iter_buf_t row_iter_buf;\n",
    "        mp_obj_t row_item, row_iterable;\n",
    "        row_iterable = mp_getiter(row_list, &row_iter_buf);\n",
    "        size_t i = 0;\n",
    "        rindex = 0;\n",
    "        if(column_list == mp_const_none) { // columns are indexed by a slice\n",
    "            while((row_item = mp_iternext(row_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                if(mp_obj_is_true(row_item)) {\n",
    "                    cindex = column.start;\n",
    "                    for(size_t j=0; j < n; j++) {\n",
    "                        insert_binary_value(ndarray, rindex*ndarray->n+cindex, values, i*M*n+j*N);\n",
    "                        cindex += column.step;\n",
    "                    }\n",
    "                    i++;\n",
    "                }\n",
    "                rindex++;\n",
    "            } \n",
    "        } else { // columns are indexed by a list\n",
    "            mp_obj_iter_buf_t column_iter_buf;\n",
    "            mp_obj_t column_item, column_iterable;\n",
    "            size_t j = 0, cindex = 0;\n",
    "            while((row_item = mp_iternext(row_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                if(mp_obj_is_true(row_item)) {\n",
    "                    column_iterable = mp_getiter(column_list, &column_iter_buf);                   \n",
    "                    while((column_item = mp_iternext(column_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                        if(mp_obj_is_true(column_item)) {\n",
    "                            insert_binary_value(ndarray, rindex*ndarray->n+cindex, values, i*M*n+j*N);\n",
    "                            j++;\n",
    "                        }\n",
    "                        cindex++;\n",
    "                    }\n",
    "                    i++;\n",
    "                }\n",
    "                rindex++;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    return mp_const_none;\n",
    "}\n",
    "\n",
    "mp_obj_t iterate_slice_list(ndarray_obj_t *ndarray, size_t m, size_t n, \n",
    "                            mp_bound_slice_t row, mp_bound_slice_t column, \n",
    "                            mp_obj_t row_list, mp_obj_t column_list, \n",
    "                            ndarray_obj_t *values) {\n",
    "    if((m == 0) || (n == 0)) {\n",
    "        mp_raise_msg(&mp_type_IndexError, \"empty index range\");\n",
    "    }\n",
    "\n",
    "    if(values != NULL) {\n",
    "        return insert_slice_list(ndarray, m, n, row, column, row_list, column_list, values);\n",
    "    }\n",
    "    uint8_t _sizeof = mp_binary_get_size('@', ndarray->array->typecode, NULL);\n",
    "    ndarray_obj_t *out = create_new_ndarray(m, n, ndarray->array->typecode);\n",
    "    uint8_t *target = (uint8_t *)out->array->items;\n",
    "    uint8_t *source = (uint8_t *)ndarray->array->items;\n",
    "    size_t cindex, rindex;    \n",
    "    if(row_list == mp_const_none) { // rows are indexed by a slice\n",
    "        rindex = row.start;\n",
    "        if(column_list == mp_const_none) { // columns are indexed by a slice\n",
    "            for(size_t i=0; i < m; i++) {\n",
    "                cindex = column.start;\n",
    "                for(size_t j=0; j < n; j++) {\n",
    "                    memcpy(target+(i*n+j)*_sizeof, source+(rindex*ndarray->n+cindex)*_sizeof, _sizeof);\n",
    "                    cindex += column.step;\n",
    "                }\n",
    "                rindex += row.step;\n",
    "            }\n",
    "        } else { // columns are indexed by a Boolean list\n",
    "            // TODO: the list must be exactly as long as the axis\n",
    "            mp_obj_iter_buf_t column_iter_buf;\n",
    "            mp_obj_t column_item, column_iterable;\n",
    "            for(size_t i=0; i < m; i++) {\n",
    "                column_iterable = mp_getiter(column_list, &column_iter_buf);\n",
    "                size_t j = 0;\n",
    "                cindex = 0;\n",
    "                while((column_item = mp_iternext(column_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                    if(mp_obj_is_true(column_item)) {\n",
    "                        memcpy(target+(i*n+j)*_sizeof, source+(rindex*ndarray->n+cindex)*_sizeof, _sizeof);\n",
    "                        j++;\n",
    "                    }\n",
    "                    cindex++;\n",
    "                }\n",
    "                rindex += row.step;\n",
    "            }\n",
    "        }\n",
    "    } else { // rows are indexed by a Boolean list\n",
    "        mp_obj_iter_buf_t row_iter_buf;\n",
    "        mp_obj_t row_item, row_iterable;\n",
    "        row_iterable = mp_getiter(row_list, &row_iter_buf);\n",
    "        size_t i = 0;\n",
    "        rindex = 0;\n",
    "        if(column_list == mp_const_none) { // columns are indexed by a slice\n",
    "            while((row_item = mp_iternext(row_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                if(mp_obj_is_true(row_item)) {\n",
    "                    cindex = column.start;\n",
    "                    for(size_t j=0; j < n; j++) {\n",
    "                        memcpy(target+(i*n+j)*_sizeof, source+(rindex*ndarray->n+cindex)*_sizeof, _sizeof);\n",
    "                        cindex += column.step;\n",
    "                    }\n",
    "                    i++;\n",
    "                }\n",
    "                rindex++;\n",
    "            } \n",
    "        } else { // columns are indexed by a list\n",
    "            mp_obj_iter_buf_t column_iter_buf;\n",
    "            mp_obj_t column_item, column_iterable;\n",
    "            size_t j = 0, cindex = 0;\n",
    "            while((row_item = mp_iternext(row_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                if(mp_obj_is_true(row_item)) {\n",
    "                    column_iterable = mp_getiter(column_list, &column_iter_buf);                   \n",
    "                    while((column_item = mp_iternext(column_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                        if(mp_obj_is_true(column_item)) {\n",
    "                            memcpy(target+(i*n+j)*_sizeof, source+(rindex*ndarray->n+cindex)*_sizeof, _sizeof);\n",
    "                            j++;\n",
    "                        }\n",
    "                        cindex++;\n",
    "                    }\n",
    "                    i++;\n",
    "                }\n",
    "                rindex++;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_get_slice(ndarray_obj_t *ndarray, mp_obj_t index, ndarray_obj_t *values) {\n",
    "    mp_bound_slice_t row_slice = simple_slice(0, 0, 1), column_slice = simple_slice(0, 0, 1);\n",
    "\n",
    "    size_t m = 0, n = 0;\n",
    "    if(mp_obj_is_int(index) && (ndarray->m == 1) && (values == NULL)) { \n",
    "        // we have a row vector, and don't want to assign\n",
    "        column_slice = generate_slice(ndarray->n, index);\n",
    "        if(slice_length(column_slice) == 1) { // we were asked for a single item\n",
    "            // subscribe returns an mp_obj_t, if and only, if the index is an integer, and we have a row vector\n",
    "            return mp_binary_get_val_array(ndarray->array->typecode, ndarray->array->items, column_slice.start);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    if(mp_obj_is_int(index) || MP_OBJ_IS_TYPE(index, &mp_type_slice)) {\n",
    "        if(ndarray->m == 1) { // we have a row vector\n",
    "            column_slice = generate_slice(ndarray->n, index);\n",
    "            row_slice = simple_slice(0, 1, 1);\n",
    "        } else { // we have a matrix\n",
    "            row_slice = generate_slice(ndarray->m, index);\n",
    "            column_slice = simple_slice(0, ndarray->n, 1); // take all columns\n",
    "        }\n",
    "        m = slice_length(row_slice);\n",
    "        n = slice_length(column_slice);\n",
    "        return iterate_slice_list(ndarray, m, n, row_slice, column_slice, mp_const_none, mp_const_none, values);\n",
    "    } else if(MP_OBJ_IS_TYPE(index, &mp_type_list)) {\n",
    "        n = true_length(index);\n",
    "        if(ndarray->m == 1) { // we have a flat array\n",
    "            // we might have to separate the n == 1 case\n",
    "            row_slice = simple_slice(0, 1, 1);\n",
    "            return iterate_slice_list(ndarray, 1, n, row_slice, column_slice, mp_const_none, index, values);\n",
    "        } else { // we have a matrix\n",
    "            return iterate_slice_list(ndarray, 1, n, row_slice, column_slice, mp_const_none, index, values);\n",
    "        }\n",
    "    }\n",
    "    else { // we certainly have a tuple, so let us deal with it\n",
    "        mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(index);\n",
    "        if(tuple->len != 2) {\n",
    "            mp_raise_msg(&mp_type_IndexError, \"too many indices\");\n",
    "        }\n",
    "        if(!(MP_OBJ_IS_TYPE(tuple->items[0], &mp_type_list) || \n",
    "            MP_OBJ_IS_TYPE(tuple->items[0], &mp_type_slice) || \n",
    "            mp_obj_is_int(tuple->items[0])) || \n",
    "           !(MP_OBJ_IS_TYPE(tuple->items[1], &mp_type_list) || \n",
    "            MP_OBJ_IS_TYPE(tuple->items[1], &mp_type_slice) || \n",
    "            mp_obj_is_int(tuple->items[1]))) {\n",
    "                mp_raise_msg(&mp_type_IndexError, \"indices must be integers, slices, or Boolean lists\");\n",
    "        }\n",
    "        if(MP_OBJ_IS_TYPE(tuple->items[0], &mp_type_list)) { // rows are indexed by Boolean list\n",
    "            m = true_length(tuple->items[0]);\n",
    "            if(MP_OBJ_IS_TYPE(tuple->items[1], &mp_type_list)) {\n",
    "                n = true_length(tuple->items[1]);\n",
    "                return iterate_slice_list(ndarray, m, n, row_slice, column_slice, \n",
    "                                          tuple->items[0], tuple->items[1], values);\n",
    "            } else { // the column is indexed by an integer, or a slice\n",
    "                column_slice = generate_slice(ndarray->n, tuple->items[1]);\n",
    "                n = slice_length(column_slice);\n",
    "                return iterate_slice_list(ndarray, m, n, row_slice, column_slice, \n",
    "                                          tuple->items[0], mp_const_none, values);\n",
    "            }\n",
    "            \n",
    "        } else { // rows are indexed by a slice, or an integer\n",
    "            row_slice = generate_slice(ndarray->m, tuple->items[0]);\n",
    "            m = slice_length(row_slice);\n",
    "            if(MP_OBJ_IS_TYPE(tuple->items[1], &mp_type_list)) { // columns are indexed by a Boolean list\n",
    "                n = true_length(tuple->items[1]);\n",
    "                return iterate_slice_list(ndarray, m, n, row_slice, column_slice, \n",
    "                                         mp_const_none, tuple->items[1], values);\n",
    "            } else { // columns are indexed by an integer, or a slice\n",
    "                column_slice = generate_slice(ndarray->n, tuple->items[1]);\n",
    "                n = slice_length(column_slice);\n",
    "                return iterate_slice_list(ndarray, m, n, row_slice, column_slice, \n",
    "                                          mp_const_none, mp_const_none, values);             \n",
    "                \n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    \n",
    "    if (value == MP_OBJ_SENTINEL) { // return value(s)\n",
    "        return ndarray_get_slice(self, index, NULL);    \n",
    "    } else { // assignment to slices; the value must be an ndarray, or a scalar\n",
    "        if(!MP_OBJ_IS_TYPE(value, &ulab_ndarray_type) && \n",
    "          !mp_obj_is_int(value) && !mp_obj_is_float(value)) {\n",
    "            mp_raise_ValueError(\"right hand side must be an ndarray, or a scalar\");\n",
    "        } else {\n",
    "            ndarray_obj_t *values = NULL;\n",
    "            if(mp_obj_is_int(value)) {\n",
    "                values = create_new_ndarray(1, 1, self->array->typecode);\n",
    "                mp_binary_set_val_array(values->array->typecode, values->array->items, 0, value);   \n",
    "            } else if(mp_obj_is_float(value)) {\n",
    "                values = create_new_ndarray(1, 1, NDARRAY_FLOAT);\n",
    "                mp_binary_set_val_array(NDARRAY_FLOAT, values->array->items, 0, value);\n",
    "            } else {\n",
    "                values = MP_OBJ_TO_PTR(value);\n",
    "            }\n",
    "            return ndarray_get_slice(self, index, values);\n",
    "        }\n",
    "    }      \n",
    "    return mp_const_none;\n",
    "}\n",
    "\n",
    "// itarray iterator\n",
    "\n",
    "mp_obj_t ndarray_getiter(mp_obj_t o_in, mp_obj_iter_buf_t *iter_buf) {\n",
    "    return mp_obj_new_ndarray_iterator(o_in, 0, iter_buf);\n",
    "}\n",
    "\n",
    "typedef struct _mp_obj_ndarray_it_t {\n",
    "    mp_obj_base_t base;\n",
    "    mp_fun_1_t iternext;\n",
    "    mp_obj_t ndarray;\n",
    "    size_t cur;\n",
    "} mp_obj_ndarray_it_t;\n",
    "\n",
    "mp_obj_t ndarray_iternext(mp_obj_t self_in) {\n",
    "    mp_obj_ndarray_it_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(self->ndarray);\n",
    "    // TODO: in numpy, ndarrays are iterated with respect to the first axis. \n",
    "    size_t iter_end = 0;\n",
    "    if((ndarray->m == 1)) {\n",
    "        iter_end = ndarray->array->len;\n",
    "    } else {\n",
    "        iter_end = ndarray->m;\n",
    "    }\n",
    "    if(self->cur < iter_end) {\n",
    "        if(ndarray->n == ndarray->array->len) { // we have a linear array\n",
    "            // read the current value\n",
    "            mp_obj_t value;\n",
    "            value = mp_binary_get_val_array(ndarray->array->typecode, ndarray->array->items, self->cur);\n",
    "            self->cur++;\n",
    "            return value;\n",
    "        } else { // we have a matrix, return the number of rows\n",
    "            ndarray_obj_t *value = create_new_ndarray(1, ndarray->n, ndarray->array->typecode);\n",
    "            // copy the memory content here\n",
    "            uint8_t *tmp = (uint8_t *)ndarray->array->items;\n",
    "            size_t strip_size = ndarray->n * mp_binary_get_size('@', ndarray->array->typecode, NULL);\n",
    "            memcpy(value->array->items, &tmp[self->cur*strip_size], strip_size);\n",
    "            self->cur++;\n",
    "            return value;\n",
    "        }\n",
    "    } else {\n",
    "        return MP_OBJ_STOP_ITERATION;\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t mp_obj_new_ndarray_iterator(mp_obj_t ndarray, size_t cur, mp_obj_iter_buf_t *iter_buf) {\n",
    "    assert(sizeof(mp_obj_ndarray_it_t) <= sizeof(mp_obj_iter_buf_t));\n",
    "    mp_obj_ndarray_it_t *o = (mp_obj_ndarray_it_t*)iter_buf;\n",
    "    o->base.type = &mp_type_polymorph_iter;\n",
    "    o->iternext = ndarray_iternext;\n",
    "    o->ndarray = ndarray;\n",
    "    o->cur = cur;\n",
    "    return MP_OBJ_FROM_PTR(o);\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_shape(mp_obj_t self_in) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    mp_obj_t tuple[2] = {\n",
    "        mp_obj_new_int(self->m),\n",
    "        mp_obj_new_int(self->n)\n",
    "    };\n",
    "    return mp_obj_new_tuple(2, tuple);\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_rawsize(mp_obj_t self_in) {\n",
    "    // returns a 5-tuple with the \n",
    "    // \n",
    "    // 0. number of rows\n",
    "    // 1. number of columns\n",
    "    // 2. length of the storage (should be equal to the product of 1. and 2.)\n",
    "    // 3. length of the data storage in bytes\n",
    "    // 4. datum size in bytes\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(5, NULL));\n",
    "    tuple->items[0] = MP_OBJ_NEW_SMALL_INT(self->m);\n",
    "    tuple->items[1] = MP_OBJ_NEW_SMALL_INT(self->n);\n",
    "    tuple->items[2] = MP_OBJ_NEW_SMALL_INT(self->array->len);\n",
    "    tuple->items[3] = MP_OBJ_NEW_SMALL_INT(self->bytes);\n",
    "    tuple->items[4] = MP_OBJ_NEW_SMALL_INT(mp_binary_get_size('@', self->array->typecode, NULL));\n",
    "    return tuple;\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_flatten(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_order, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_QSTR(MP_QSTR_C)} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    mp_obj_t self_copy = ndarray_copy(pos_args[0]);\n",
    "    ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(self_copy);\n",
    "    \n",
    "    GET_STR_DATA_LEN(args[0].u_obj, order, len);    \n",
    "    if((len != 1) || ((memcmp(order, \"C\", 1) != 0) && (memcmp(order, \"F\", 1) != 0))) {\n",
    "        mp_raise_ValueError(\"flattening order must be either 'C', or 'F'\");        \n",
    "    }\n",
    "\n",
    "    // if order == 'C', we simply have to set m, and n, there is nothing else to do\n",
    "    if(memcmp(order, \"F\", 1) == 0) {\n",
    "        ndarray_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);\n",
    "        uint8_t _sizeof = mp_binary_get_size('@', self->array->typecode, NULL);\n",
    "        // get the data of self_in: we won't need a temporary buffer for the transposition\n",
    "        uint8_t *self_array = (uint8_t *)self->array->items;\n",
    "        uint8_t *array = (uint8_t *)ndarray->array->items;\n",
    "        size_t i=0;\n",
    "        for(size_t n=0; n < self->n; n++) {\n",
    "            for(size_t m=0; m < self->m; m++) {\n",
    "                memcpy(array+_sizeof*i, self_array+_sizeof*(m*self->n + n), _sizeof);\n",
    "                i++;\n",
    "            }\n",
    "        }        \n",
    "    }\n",
    "    ndarray->n = ndarray->array->len;\n",
    "    ndarray->m = 1;\n",
    "    return self_copy;\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_asbytearray(mp_obj_t self_in) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    return MP_OBJ_FROM_PTR(self->array);\n",
    "}\n",
    "\n",
    "// Binary operations\n",
    "\n",
    "mp_obj_t ndarray_binary_op(mp_binary_op_t op, mp_obj_t lhs, mp_obj_t rhs) {\n",
    "//    if(op == MP_BINARY_OP_REVERSE_ADD) {\n",
    " //       return ndarray_binary_op(MP_BINARY_OP_ADD, rhs, lhs);\n",
    "  //  }    \n",
    "    // One of the operands is a scalar\n",
    "    // TODO: conform to numpy with the upcasting\n",
    "    // TODO: implement in-place operators\n",
    "    mp_obj_t RHS = MP_OBJ_NULL;\n",
    "    bool rhs_is_scalar = true;\n",
    "    if(mp_obj_is_int(rhs)) {\n",
    "        int32_t ivalue = mp_obj_get_int(rhs);\n",
    "        if((ivalue > 0) && (ivalue < 256)) {\n",
    "            CREATE_SINGLE_ITEM(RHS, uint8_t, NDARRAY_UINT8, ivalue);\n",
    "        } else if((ivalue > 255) && (ivalue < 65535)) {\n",
    "            CREATE_SINGLE_ITEM(RHS, uint16_t, NDARRAY_UINT16, ivalue);\n",
    "        } else if((ivalue < 0) && (ivalue > -128)) {\n",
    "            CREATE_SINGLE_ITEM(RHS, int8_t, NDARRAY_INT8, ivalue);\n",
    "        } else if((ivalue < -127) && (ivalue > -32767)) {\n",
    "            CREATE_SINGLE_ITEM(RHS, int16_t, NDARRAY_INT16, ivalue);\n",
    "        } else { // the integer value clearly does not fit the ulab types, so move on to float\n",
    "            CREATE_SINGLE_ITEM(RHS, mp_float_t, NDARRAY_FLOAT, ivalue);\n",
    "        }\n",
    "    } else if(mp_obj_is_float(rhs)) {\n",
    "        mp_float_t fvalue = mp_obj_get_float(rhs);        \n",
    "        CREATE_SINGLE_ITEM(RHS, mp_float_t, NDARRAY_FLOAT, fvalue);\n",
    "    } else {\n",
    "        RHS = rhs;\n",
    "        rhs_is_scalar = false;\n",
    "    }\n",
    "    //else \n",
    "    if(mp_obj_is_type(lhs, &ulab_ndarray_type) && mp_obj_is_type(RHS, &ulab_ndarray_type)) { \n",
    "        // next, the ndarray stuff\n",
    "        ndarray_obj_t *ol = MP_OBJ_TO_PTR(lhs);\n",
    "        ndarray_obj_t *or = MP_OBJ_TO_PTR(RHS);\n",
    "        if(!rhs_is_scalar && ((ol->m != or->m) || (ol->n != or->n))) {\n",
    "            mp_raise_ValueError(\"operands could not be broadcast together\");\n",
    "        }\n",
    "        // At this point, the operands should have the same shape\n",
    "        switch(op) {\n",
    "            case MP_BINARY_OP_EQUAL:\n",
    "                // Two arrays are equal, if their shape, typecode, and elements are equal\n",
    "                if((ol->m != or->m) || (ol->n != or->n) || (ol->array->typecode != or->array->typecode)) {\n",
    "                    return mp_const_false;\n",
    "                } else {\n",
    "                    size_t i = ol->bytes;\n",
    "                    uint8_t *l = (uint8_t *)ol->array->items;\n",
    "                    uint8_t *r = (uint8_t *)or->array->items;\n",
    "                    while(i) { // At this point, we can simply compare the bytes, the type is irrelevant\n",
    "                        if(*l++ != *r++) {\n",
    "                            return mp_const_false;\n",
    "                        }\n",
    "                        i--;\n",
    "                    }\n",
    "                    return mp_const_true;\n",
    "                }\n",
    "                break;\n",
    "            case MP_BINARY_OP_LESS:\n",
    "            case MP_BINARY_OP_LESS_EQUAL:\n",
    "            case MP_BINARY_OP_MORE:\n",
    "            case MP_BINARY_OP_MORE_EQUAL:\n",
    "            case MP_BINARY_OP_ADD:\n",
    "            case MP_BINARY_OP_SUBTRACT:\n",
    "            case MP_BINARY_OP_TRUE_DIVIDE:\n",
    "            case MP_BINARY_OP_MULTIPLY:\n",
    "                // TODO: I believe, this part can be made significantly smaller (compiled size)\n",
    "                // by doing only the typecasting in the large ifs, and moving the loops outside\n",
    "                // These are the upcasting rules\n",
    "                // float always becomes float\n",
    "                // operation on identical types preserves type\n",
    "                // uint8 + int8 => int16\n",
    "                // uint8 + int16 => int16\n",
    "                // uint8 + uint16 => uint16\n",
    "                // int8 + int16 => int16\n",
    "                // int8 + uint16 => uint16\n",
    "                // uint16 + int16 => float\n",
    "                // The parameters of RUN_BINARY_LOOP are \n",
    "                // typecode of result, type_out, type_left, type_right, lhs operand, rhs operand, operator\n",
    "                if(ol->array->typecode == NDARRAY_UINT8) {\n",
    "                    if(or->array->typecode == NDARRAY_UINT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_UINT8, uint8_t, uint8_t, uint8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, uint8_t, int8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_UINT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_UINT16, uint16_t, uint8_t, uint16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, uint8_t, int16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_FLOAT) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, uint8_t, mp_float_t, ol, or, op);\n",
    "                    }\n",
    "                } else if(ol->array->typecode == NDARRAY_INT8) {\n",
    "                    if(or->array->typecode == NDARRAY_UINT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int8_t, uint8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT8, int8_t, int8_t, int8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_UINT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int8_t, uint16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int8_t, int16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_FLOAT) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, int8_t, mp_float_t, ol, or, op);\n",
    "                    }                \n",
    "                } else if(ol->array->typecode == NDARRAY_UINT16) {\n",
    "                    if(or->array->typecode == NDARRAY_UINT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_UINT16, uint16_t, uint16_t, uint8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_UINT16, uint16_t, uint16_t, int8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_UINT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_UINT16, uint16_t, uint16_t, uint16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, uint16_t, int16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_FLOAT) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, uint8_t, mp_float_t, ol, or, op);\n",
    "                    }\n",
    "                } else if(ol->array->typecode == NDARRAY_INT16) {\n",
    "                    if(or->array->typecode == NDARRAY_UINT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int16_t, uint8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int16_t, int8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_UINT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, int16_t, uint16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_INT16, int16_t, int16_t, int16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_FLOAT) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, uint16_t, mp_float_t, ol, or, op);\n",
    "                    }\n",
    "                } else if(ol->array->typecode == NDARRAY_FLOAT) {\n",
    "                    if(or->array->typecode == NDARRAY_UINT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, mp_float_t, uint8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT8) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, mp_float_t, int8_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_UINT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, mp_float_t, uint16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_INT16) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, mp_float_t, int16_t, ol, or, op);\n",
    "                    } else if(or->array->typecode == NDARRAY_FLOAT) {\n",
    "                        RUN_BINARY_LOOP(NDARRAY_FLOAT, mp_float_t, mp_float_t, mp_float_t, ol, or, op);\n",
    "                    }\n",
    "                } else { // this should never happen\n",
    "                    mp_raise_TypeError(\"wrong input type\");\n",
    "                }\n",
    "                // this instruction should never be reached, but we have to make the compiler happy\n",
    "                return MP_OBJ_NULL; \n",
    "            default:\n",
    "                return MP_OBJ_NULL; // op not supported                                                        \n",
    "        }\n",
    "    } else {\n",
    "        mp_raise_TypeError(\"wrong operand type on the right hand side\");\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t ndarray_unary_op(mp_unary_op_t op, mp_obj_t self_in) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    ndarray_obj_t *ndarray = NULL;\n",
    "    switch (op) {\n",
    "        case MP_UNARY_OP_LEN: \n",
    "            if(self->m > 1) {\n",
    "                return mp_obj_new_int(self->m);\n",
    "            } else {\n",
    "                return mp_obj_new_int(self->n);\n",
    "            }\n",
    "            break;\n",
    "        \n",
    "        case MP_UNARY_OP_INVERT:\n",
    "            if(self->array->typecode == NDARRAY_FLOAT) {\n",
    "                mp_raise_ValueError(\"operation is not supported for given type\");\n",
    "            }\n",
    "            // we can invert the content byte by byte, there is no need to distinguish \n",
    "            // between different typecodes\n",
    "            ndarray = MP_OBJ_TO_PTR(ndarray_copy(self_in));\n",
    "            uint8_t *array = (uint8_t *)ndarray->array->items;\n",
    "            for(size_t i=0; i < self->bytes; i++) array[i] = ~array[i];\n",
    "            return MP_OBJ_FROM_PTR(ndarray);\n",
    "            break;\n",
    "        \n",
    "        case MP_UNARY_OP_NEGATIVE:\n",
    "            ndarray = MP_OBJ_TO_PTR(ndarray_copy(self_in));\n",
    "            if(self->array->typecode == NDARRAY_UINT8) {\n",
    "                uint8_t *array = (uint8_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) array[i] = -array[i];\n",
    "            } else if(self->array->typecode == NDARRAY_INT8) {\n",
    "                int8_t *array = (int8_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) array[i] = -array[i];\n",
    "            } else if(self->array->typecode == NDARRAY_UINT16) {                \n",
    "                uint16_t *array = (uint16_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) array[i] = -array[i];\n",
    "            } else if(self->array->typecode == NDARRAY_INT16) {\n",
    "                int16_t *array = (int16_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) array[i] = -array[i];\n",
    "            } else {\n",
    "                mp_float_t *array = (mp_float_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) array[i] = -array[i];\n",
    "            }\n",
    "            return MP_OBJ_FROM_PTR(ndarray);\n",
    "            break;\n",
    "\n",
    "        case MP_UNARY_OP_POSITIVE:\n",
    "            return ndarray_copy(self_in);\n",
    "\n",
    "        case MP_UNARY_OP_ABS:\n",
    "            if((self->array->typecode == NDARRAY_UINT8) || (self->array->typecode == NDARRAY_UINT16)) {\n",
    "                return ndarray_copy(self_in);\n",
    "            }\n",
    "            ndarray = MP_OBJ_TO_PTR(ndarray_copy(self_in));\n",
    "            if((self->array->typecode == NDARRAY_INT8)) {\n",
    "                int8_t *array = (int8_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) {\n",
    "                    if(array[i] < 0) array[i] = -array[i];\n",
    "                }\n",
    "            } else if((self->array->typecode == NDARRAY_INT16)) {\n",
    "                int16_t *array = (int16_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) {\n",
    "                    if(array[i] < 0) array[i] = -array[i];\n",
    "                }\n",
    "            } else {\n",
    "                mp_float_t *array = (mp_float_t *)ndarray->array->items;\n",
    "                for(size_t i=0; i < self->array->len; i++) {\n",
    "                    if(array[i] < 0) array[i] = -array[i];\n",
    "                }                \n",
    "            }\n",
    "            return MP_OBJ_FROM_PTR(ndarray);\n",
    "            break;\n",
    "        default: return MP_OBJ_NULL; // operator not supported\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:22:50.573475Z",
     "start_time": "2019-10-29T07:22:50.557753Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([9, 10, 11, 12], dtype=uint8)\n",
      "(1, 4)\n",
      "16\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab as np\n",
    "\n",
    "a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]], dtype=np.uint8)\n",
    "b = a[:1:-2]\n",
    "print(a[-2])\n",
    "print(b.shape())\n",
    "print(b[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:25:18.790051Z",
     "start_time": "2019-10-29T07:25:18.774740Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([[0.0, 0.0, 0.0, 0.0, 0.0],\n",
      "\t [0.0, 3.0, 4.0, 0.0, 0.0],\n",
      "\t [0.0, 3.0, 4.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0, 0.0, 0.0]], dtype=float) array([0.0, 3.0, 4.0, 0.0, 0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab as np\n",
    "\n",
    "a = np.zeros((5, 5))\n",
    "a[1:3, 1:3] = np.array([3.0, 4.0])\n",
    "print(a, a[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-29T07:23:05.570117Z",
     "start_time": "2019-10-29T07:23:05.555229Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], dtype=float)\n",
      "array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], dtype=float) 3.0\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab as np\n",
    "\n",
    "a = np.zeros(8)\n",
    "print(a)\n",
    "\n",
    "for i in range(8):\n",
    "    a[i] = i\n",
    "    \n",
    "# a[0] = 123\n",
    "print(a, a[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T19:45:37.334634Z",
     "start_time": "2019-10-10T19:45:37.321718Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "a = ones((10,3))\n",
    "a[2:7:2] = zeros(3)\n",
    "print(a)\n",
    "a = ones((4, 4))\n",
    "a[1] = 0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1272,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-11T15:47:30.218758Z",
     "start_time": "2019-10-11T15:47:30.200960Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([3.0, 5.0, 7.0], dtype=float)\n",
      "array([1.0, 2.0, 13.0, 4.0, 13.0, 6.0, 13.0, 8.0, 9.0], dtype=float)\n",
      "array([13.0, 4.0, 13.0, 6.0, 13.0, 8.0, 9.0], dtype=float)\n",
      "array([[1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0]], dtype=float)\n",
      "array([[1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [2.0, 2.0, 2.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [2.0, 2.0, 2.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [2.0, 2.0, 2.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "\n",
    "import ulab as np\n",
    "\n",
    "a = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=np.float)\n",
    "print(a[2:7:2])\n",
    "a[2:7:2] = 13.0\n",
    "print(a)\n",
    "\n",
    "print(a[a > 3])\n",
    "a = np.ones((10,3))\n",
    "a[2:7:2] = np.zeros(3)\n",
    "print(a)\n",
    "\n",
    "a = np.ones((10,3))\n",
    "a[2:7:2] = 2.0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1135,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T05:44:27.822834Z",
     "start_time": "2019-10-10T05:44:27.817682Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([10,  1, 20,  3, 30,  5, 40,  7,  8,  9], dtype=int8)"
      ]
     },
     "execution_count": 1135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8)\n",
    "a[:8:2] = array([10, 20, 30, 40], dtype=float)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1138,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-10T05:55:00.676258Z",
     "start_time": "2019-10-10T05:55:00.640557Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 6])"
      ]
     },
     "execution_count": 1138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])\n",
    "a[1, :2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 438,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-05T06:37:27.232755Z",
     "start_time": "2019-10-05T06:37:27.222656Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True,  True, False, False])"
      ]
     },
     "execution_count": 438,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([1, 2, 3, 4])\n",
    "a < a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 973,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:11:22.698682Z",
     "start_time": "2019-10-08T15:11:22.681644Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([1, 2, 3], dtype=int8)\n",
      "ndarray([254, 253, 252], dtype=uint8)\n",
      "ndarray([-2, -3, -4], dtype=int8)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([1, -2, 3], dtype=ulab.int8)\n",
    "\n",
    "print(abs(a))\n",
    "\n",
    "a = ulab.ndarray([1, 2, 3], dtype=ulab.uint8)\n",
    "print(~a)\n",
    "\n",
    "a = ulab.ndarray([1, 2, 3], dtype=ulab.int8)\n",
    "print(~a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 999,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:30:44.615366Z",
     "start_time": "2019-10-08T15:30:44.608578Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-1  2 -3] [ 1 -2  3]\n",
      "[255 254 253] [1 2 3]\n",
      "[-1. -2.  3.] [ 1.  2. -3.]\n"
     ]
    }
   ],
   "source": [
    "a = array([1, -2, 3], dtype=int8)\n",
    "print(-a, +a)\n",
    "\n",
    "a = array([1, 2, 3], dtype=uint8)\n",
    "print(-a, +a)\n",
    "\n",
    "a = array([1, 2, -3], dtype=float)\n",
    "print(-a, +a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1003,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:32:26.308964Z",
     "start_time": "2019-10-08T15:32:26.286878Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([-1, 2, -3], dtype=int8) ndarray([1, -2, 3], dtype=int8)\n",
      "ndarray([255, 254, 253], dtype=uint8) ndarray([1, 2, 3], dtype=uint8)\n",
      "ndarray([-1.0, -2.0, 3.0], dtype=float) ndarray([1.0, 2.0, -3.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([1, -2, 3], dtype=ulab.int8)\n",
    "print(-a, +a)\n",
    "\n",
    "a = ulab.ndarray([1, 2, 3], dtype=ulab.uint8)\n",
    "print(-a, +a)\n",
    "\n",
    "a = ulab.ndarray([1, 2, -3], dtype=ulab.float)\n",
    "print(-a, +a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 917,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T05:28:23.115294Z",
     "start_time": "2019-10-08T05:28:23.109632Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2 -3 -4]\n"
     ]
    }
   ],
   "source": [
    "a = array([1, 2, 3], dtype=int8)\n",
    "\n",
    "print(~a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-04T13:58:54.784489Z",
     "start_time": "2019-10-04T13:58:54.781544Z"
    }
   },
   "outputs": [],
   "source": [
    "a = array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-04T14:01:41.632035Z",
     "start_time": "2019-10-04T14:01:41.627105Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0, 1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Linear algebra\n",
    "\n",
    "This module contains very basic matrix operators, such as transposing, reshaping, inverting, and matrix multiplication. The actual inversion is factored out into a helper function, so that the routine can be re-used in other modules. (The `polyfit` function in `poly.c` uses that.) Also note that inversion is based on the notion of a *small number* (epsilon). During the computation of the inverse, a number is treated as 0, if its absolute value is smaller than epsilon. This precaution is required, otherwise, one might run into singular matrices. \n",
    "\n",
    "As in `numpy`, `inv` is not a class method, but it should be applied only on `ndarray`s. This is why one has to check the argument type at the beginning of the functions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Transpose of one- and two-dimensional arrays, .transpose()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:39:29.580358Z",
     "start_time": "2019-09-18T19:39:29.561067Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0], dtype=float)\n",
      "shape of a:  (10, 1)\n",
      "\n",
      "transpose of array:  ndarray([0.0, 1.0, 2.0, ..., 7.0, 8.0, 9.0], dtype=float)\n",
      "shape of a:  (1, 10)\n",
      "\n",
      "2D array: \n",
      " ndarray([[1.0, 2.0, 3.0, 4.0],\n",
      "\t [5.0, 6.0, 7.0, 8.0]], dtype=float)\n",
      "shape of a:  (2, 4)\n",
      "\n",
      "transpose of array: \n",
      " ndarray([[1.0, 5.0],\n",
      "\t [2.0, 6.0],\n",
      "\t [3.0, 7.0],\n",
      "\t [4.0, 8.0]], dtype=float)\n",
      "shape of a:  (4, 2)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "a = ndarray(range(10))\n",
    "print('1D array: ', a)\n",
    "print('shape of a: ', a.shape())\n",
    "\n",
    "a.transpose()\n",
    "print('\\ntranspose of array: ', a)\n",
    "print('shape of a: ', a.shape())\n",
    "\n",
    "\n",
    "a = ndarray([[1, 2, 3, 4], [5, 6, 7, 8]])\n",
    "print('\\n2D array: \\n', a)\n",
    "print('shape of a: ', a.shape())\n",
    "\n",
    "a.transpose()\n",
    "print('\\ntranspose of array: \\n', a)\n",
    "print('shape of a: ', a.shape())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### .reshape()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:39:11.121072Z",
     "start_time": "2019-09-18T19:39:11.107269Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([0.0, 1.0, 2.0, ..., 12.0, 13.0, 14.0], dtype=float)\n",
      "shape of a:  (15, 1)\n",
      "\n",
      "2D array: \n",
      " ndarray([[0.0, 1.0, 2.0, 3.0, 4.0],\n",
      "\t [5.0, 6.0, 7.0, 8.0, 9.0],\n",
      "\t [10.0, 11.0, 12.0, 13.0, 14.0]], dtype=float)\n",
      "shape of a:  (3, 5)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray\n",
    "\n",
    "a = ndarray(range(15))\n",
    "print('1D array: ', a)\n",
    "print('shape of a: ', a.shape())\n",
    "\n",
    "a.reshape((3, 5))\n",
    "print('\\n2D array: \\n', a)\n",
    "print('shape of a: ', a.shape())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### inverse of a matrix (inv)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 192,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-20T04:57:35.940304Z",
     "start_time": "2019-09-20T04:57:35.923239Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2D matrix (a): \n",
      " ndarray([[1.0, 2.0],\n",
      "\t [3.0, 4.0]], dtype=float)\n",
      "\n",
      "inverse of a: \n",
      " ndarray([[-2.0, 1.0],\n",
      "\t [1.5, -0.5]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray, inv\n",
    "\n",
    "a = ndarray([[1, 2], [3, 4]])\n",
    "print('2D matrix (a): \\n', a)\n",
    "b = inv(a)\n",
    "print('\\ninverse of a: \\n', b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### matrix multiplication (dot)\n",
    "\n",
    "With the `dot` function, we can now check, whether the inverse of the matrix was correct:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 216,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-20T14:31:34.766022Z",
     "start_time": "2019-09-20T14:31:34.750213Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2D matrix (a): \n",
      " ndarray([[1.0, 2.0],\n",
      "\t [3.0, 4.0]], dtype=float)\n",
      "\n",
      "inverse of a: \n",
      " ndarray([[-2.0, 1.0],\n",
      "\t [1.5, -0.5]], dtype=float)\n",
      "\n",
      "a multiplied by its inverse: \n",
      " ndarray([[1.0, 0.0],\n",
      "\t [0.0, 1.0]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "from ulab import ndarray, inv, dot\n",
    "\n",
    "\n",
    "a = ndarray([[1, 2], [3, 4]])\n",
    "print('2D matrix (a): \\n', a)\n",
    "b = inv(a)\n",
    "print('\\ninverse of a: \\n', b)\n",
    "\n",
    "c = dot(a, b)\n",
    "print('\\na multiplied by its inverse: \\n', c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### zeros, ones, eye"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 366,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-04T18:02:53.444634Z",
     "start_time": "2019-10-04T18:02:53.430586Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([0, 0, 0], dtype=int16)\n",
      "ndarray([[0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 0.0]], dtype=float)\n",
      "\n",
      "====================\n",
      "\n",
      "ndarray([1, 1, 1], dtype=int16)\n",
      "ndarray([[1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0],\n",
      "\t [1.0, 1.0, 1.0]], dtype=float)\n",
      "\n",
      "====================\n",
      "\n",
      "ndarray([[1, 0, 0, 0, 0],\n",
      "\t [0, 1, 0, 0, 0],\n",
      "\t [0, 0, 1, 0, 0],\n",
      "\t [0, 0, 0, 1, 0],\n",
      "\t [0, 0, 0, 0, 1]], dtype=int16)\n",
      "ndarray([[1.0, 0.0, 0.0, 0.0, 0.0],\n",
      "\t [0.0, 1.0, 0.0, 0.0, 0.0],\n",
      "\t [0.0, 0.0, 1.0, 0.0, 0.0]], dtype=float)\n",
      "ndarray([[0, 1, 0, 0, 0],\n",
      "\t [0, 0, 1, 0, 0],\n",
      "\t [0, 0, 0, 1, 0],\n",
      "\t [0, 0, 0, 0, 1],\n",
      "\t [0, 0, 0, 0, 0]], dtype=uint8)\n",
      "ndarray([[0, 0, 0, 0, 0],\n",
      "\t [0, 0, 0, 0, 0],\n",
      "\t [0, 0, 0, 0, 0],\n",
      "\t [1, 0, 0, 0, 0],\n",
      "\t [0, 1, 0, 0, 0]], dtype=uint8)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "print(ulab.zeros(3, dtype=ulab.int16))\n",
    "print(ulab.zeros((5, 3), dtype=ulab.float))\n",
    "\n",
    "print(\"\\n====================\\n\");\n",
    "print(ulab.ones(3, dtype=ulab.int16))\n",
    "print(ulab.ones((5, 3), dtype=ulab.float))\n",
    "\n",
    "print(\"\\n====================\\n\");\n",
    "print(ulab.eye(5, dtype=ulab.int16))\n",
    "print(ulab.eye(5, M=3, dtype=ulab.float))\n",
    "\n",
    "print(ulab.eye(5, k=1, dtype=ulab.uint8))\n",
    "print(ulab.eye(5, k=-3, dtype=ulab.uint8))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## eig\n",
    "\n",
    "A decent description of the Jacobi method can be found in http://fourier.eng.hmc.edu/e176/lectures/ch1/node1.html, otherwise, https://en.wikipedia.org/wiki/Jacobi_eigenvalue_algorithm is also useful."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1989,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-19T09:42:00.580247Z",
     "start_time": "2019-10-19T09:42:00.553053Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "array([-1.165288686752319, 0.8029366731643677, 5.585625648498535, 13.77672672271729], dtype=float)\n",
      "array([[0.8151530027389526, -0.4499613046646118, -0.1644472032785416, 0.3256030678749084],\n",
      "\t [0.2211322486400604, 0.7846922278404236, 0.08364589512348175, 0.5730286836624146],\n",
      "\t [-0.1339886337518692, -0.3100103437900543, 0.8743090033531189, 0.3486031591892242],\n",
      "\t [-0.5183368921279907, -0.292722225189209, -0.4489364922046661, 0.6664056777954102]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab as np\n",
    "\n",
    "a = np.array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8)\n",
    "# a = np.array([[1, 1, 1, 1], [1, 5, 5, 5], [1, 5, 3, 2], [1, 5, 2, 3]], dtype=np.uint8)\n",
    "# a = np.array([[1, 5.5, 1], [5.5, 16, 1], [1, 1, 5.5]])\n",
    "# a = np.array([[3, 2], [2, 1]])\n",
    "x, y = np.eig(a)\n",
    "print(x)\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1983,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-19T09:38:58.050012Z",
     "start_time": "2019-10-19T09:38:57.818669Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 1 4]\n",
      " [2 5 3 5]\n",
      " [1 3 6 1]\n",
      " [4 5 1 7]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(array([13.77672606, -1.16528837,  0.80293655,  5.58562576]),\n",
       " array([[ 0.32561419,  0.815156  ,  0.44994112, -0.16446602],\n",
       "        [ 0.57300777,  0.22113342, -0.78469926,  0.08372081],\n",
       "        [ 0.34861093, -0.13401142,  0.31007764,  0.87427868],\n",
       "        [ 0.66641421, -0.51832581,  0.29266348, -0.44897499]]))"
      ]
     },
     "execution_count": 1983,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.array([[1, 2, 1, 4], [2, 5, 3, 5], [1, 3, 6, 1], [4, 5, 1, 7]], dtype=np.uint8)\n",
    "# a = np.array([[1, 1, 1, 1], [1, 5, 5, 5], [1, 5, 3, 2], [1, 5, 2, 3]], dtype=np.uint8)\n",
    "\n",
    "# a = array([[1, 5], [5, 1]])\n",
    "# a = np.array([[1, 5.5, 1], [5.5, 16, 1], [1, 1, 5.5]])\n",
    "\n",
    "print(a)\n",
    "eig(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## linalg.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 362,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-01T12:52:24.062644Z",
     "start_time": "2019-11-01T12:52:24.057127Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 1019 bytes to linalg.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode linalg.h\n",
    "\n",
    "#ifndef _LINALG_\n",
    "#define _LINALG_\n",
    "\n",
    "#include \"ndarray.h\"\n",
    "\n",
    "#define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; }\n",
    "\n",
    "#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT\n",
    "#define epsilon        1.2e-7\n",
    "#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE\n",
    "#define epsilon        2.3e-16\n",
    "#endif\n",
    "\n",
    "#define JACOBI_MAX     20\n",
    "\n",
    "mp_obj_t linalg_transpose(mp_obj_t );\n",
    "mp_obj_t linalg_reshape(mp_obj_t , mp_obj_t );\n",
    "mp_obj_t linalg_size(size_t , const mp_obj_t *, mp_map_t *);\n",
    "bool linalg_invert_matrix(mp_float_t *, size_t );\n",
    "mp_obj_t linalg_inv(mp_obj_t );\n",
    "mp_obj_t linalg_dot(mp_obj_t , mp_obj_t );\n",
    "mp_obj_t linalg_zeros(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t linalg_ones(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t linalg_eye(size_t , const mp_obj_t *, mp_map_t *);\n",
    "\n",
    "mp_obj_t linalg_det(mp_obj_t );\n",
    "mp_obj_t linalg_eig(mp_obj_t );\n",
    "\n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## linalg.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:13:21.595401Z",
     "start_time": "2019-11-28T18:13:21.587222Z"
    },
    "code_folding": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 17691 bytes to linalg.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode linalg.c\n",
    "\n",
    "#include <stdlib.h>\n",
    "#include <string.h>\n",
    "#include <math.h>\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/misc.h\"\n",
    "#include \"linalg.h\"\n",
    "\n",
    "mp_obj_t linalg_transpose(mp_obj_t self_in) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    // the size of a single item in the array\n",
    "    uint8_t _sizeof = mp_binary_get_size('@', self->array->typecode, NULL);\n",
    "    \n",
    "    // NOTE: \n",
    "    // if the matrices are square, we can simply swap items, but \n",
    "    // generic matrices can't be transposed in place, so we have to \n",
    "    // declare a temporary variable\n",
    "    \n",
    "    // NOTE: \n",
    "    //  In the old matrix, the coordinate (m, n) is m*self->n + n\n",
    "    //  We have to assign this to the coordinate (n, m) in the new \n",
    "    //  matrix, i.e., to n*self->m + m (since the new matrix has self->m columns)\n",
    "    \n",
    "    // one-dimensional arrays can be transposed by simply swapping the dimensions\n",
    "    if((self->m != 1) && (self->n != 1)) {\n",
    "        uint8_t *c = (uint8_t *)self->array->items;\n",
    "        // self->bytes is the size of the bytearray, irrespective of the typecode\n",
    "        uint8_t *tmp = m_new(uint8_t, self->bytes);\n",
    "        for(size_t m=0; m < self->m; m++) {\n",
    "            for(size_t n=0; n < self->n; n++) {\n",
    "                memcpy(tmp+_sizeof*(n*self->m + m), c+_sizeof*(m*self->n + n), _sizeof);\n",
    "            }\n",
    "        }\n",
    "        memcpy(self->array->items, tmp, self->bytes);\n",
    "        m_del(uint8_t, tmp, self->bytes);\n",
    "    } \n",
    "    SWAP(size_t, self->m, self->n);\n",
    "    return mp_const_none;\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_reshape(mp_obj_t self_in, mp_obj_t shape) {\n",
    "    ndarray_obj_t *self = MP_OBJ_TO_PTR(self_in);\n",
    "    if(!MP_OBJ_IS_TYPE(shape, &mp_type_tuple) || (MP_OBJ_SMALL_INT_VALUE(mp_obj_len_maybe(shape)) != 2)) {\n",
    "        mp_raise_ValueError(\"shape must be a 2-tuple\");\n",
    "    }\n",
    "\n",
    "    mp_obj_iter_buf_t iter_buf;\n",
    "    mp_obj_t item, iterable = mp_getiter(shape, &iter_buf);\n",
    "    uint16_t m, n;\n",
    "    item = mp_iternext(iterable);\n",
    "    m = mp_obj_get_int(item);\n",
    "    item = mp_iternext(iterable);\n",
    "    n = mp_obj_get_int(item);\n",
    "    if(m*n != self->m*self->n) {\n",
    "        // TODO: the proper error message would be \"cannot reshape array of size %d into shape (%d, %d)\"\n",
    "        mp_raise_ValueError(\"cannot reshape array (incompatible input/output shape)\");\n",
    "    }\n",
    "    self->m = m;\n",
    "    self->n = n;\n",
    "    return MP_OBJ_FROM_PTR(self);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_size(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "\n",
    "    if(!mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"size is defined for ndarrays only\");\n",
    "    } else {\n",
    "        ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(args[0].u_obj);\n",
    "        if(args[1].u_obj == mp_const_none) {\n",
    "            return mp_obj_new_int(ndarray->array->len);\n",
    "        } else if(mp_obj_is_int(args[1].u_obj)) {\n",
    "            uint8_t ax = mp_obj_get_int(args[1].u_obj);\n",
    "            if(ax == 0) {\n",
    "                if(ndarray->m == 1) {\n",
    "                    return mp_obj_new_int(ndarray->n);\n",
    "                } else {\n",
    "                    return mp_obj_new_int(ndarray->m);                    \n",
    "                }\n",
    "            } else if(ax == 1) {\n",
    "                if(ndarray->m == 1) {\n",
    "                    mp_raise_ValueError(\"tuple index out of range\");\n",
    "                } else {\n",
    "                    return mp_obj_new_int(ndarray->n);\n",
    "                }\n",
    "            } else {\n",
    "                    mp_raise_ValueError(\"tuple index out of range\");                \n",
    "            }\n",
    "        } else {\n",
    "            mp_raise_TypeError(\"wrong argument type\");\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "bool linalg_invert_matrix(mp_float_t *data, size_t N) {\n",
    "    // returns true, of the inversion was successful, \n",
    "    // false, if the matrix is singular\n",
    "    \n",
    "    // initially, this is the unit matrix: the contents of this matrix is what \n",
    "    // will be returned after all the transformations\n",
    "    mp_float_t *unit = m_new(mp_float_t, N*N);\n",
    "\n",
    "    mp_float_t elem = 1.0;\n",
    "    // initialise the unit matrix\n",
    "    memset(unit, 0, sizeof(mp_float_t)*N*N);\n",
    "    for(size_t m=0; m < N; m++) {\n",
    "        memcpy(&unit[m*(N+1)], &elem, sizeof(mp_float_t));\n",
    "    }\n",
    "    for(size_t m=0; m < N; m++){\n",
    "        // this could be faster with ((c < epsilon) && (c > -epsilon))\n",
    "        if(MICROPY_FLOAT_C_FUN(fabs)(data[m*(N+1)]) < epsilon) {\n",
    "            m_del(mp_float_t, unit, N*N);\n",
    "            return false;\n",
    "        }\n",
    "        for(size_t n=0; n < N; n++){\n",
    "            if(m != n){\n",
    "                elem = data[N*n+m] / data[m*(N+1)];\n",
    "                for(size_t k=0; k < N; k++){\n",
    "                    data[N*n+k] -= elem * data[N*m+k];\n",
    "                    unit[N*n+k] -= elem * unit[N*m+k];\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    for(size_t m=0; m < N; m++){ \n",
    "        elem = data[m*(N+1)];\n",
    "        for(size_t n=0; n < N; n++){\n",
    "            data[N*m+n] /= elem;\n",
    "            unit[N*m+n] /= elem;\n",
    "        }\n",
    "    }\n",
    "    memcpy(data, unit, sizeof(mp_float_t)*N*N);\n",
    "    m_del(mp_float_t, unit, N*N);\n",
    "    return true;\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_inv(mp_obj_t o_in) {\n",
    "    // since inv is not a class method, we have to inspect the input argument first\n",
    "    if(!MP_OBJ_IS_TYPE(o_in, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"only ndarrays can be inverted\");\n",
    "    }\n",
    "    ndarray_obj_t *o = MP_OBJ_TO_PTR(o_in);\n",
    "    if(!MP_OBJ_IS_TYPE(o_in, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"only ndarray objects can be inverted\");\n",
    "    }\n",
    "    if(o->m != o->n) {\n",
    "        mp_raise_ValueError(\"only square matrices can be inverted\");\n",
    "    }\n",
    "    ndarray_obj_t *inverted = create_new_ndarray(o->m, o->n, NDARRAY_FLOAT);\n",
    "    mp_float_t *data = (mp_float_t *)inverted->array->items;\n",
    "    mp_obj_t elem;\n",
    "    for(size_t m=0; m < o->m; m++) { // rows first\n",
    "        for(size_t n=0; n < o->n; n++) { // columns next\n",
    "            // this could, perhaps, be done in single line... \n",
    "            // On the other hand, we probably spend little time here\n",
    "            elem = mp_binary_get_val_array(o->array->typecode, o->array->items, m*o->n+n);\n",
    "            data[m*o->n+n] = (mp_float_t)mp_obj_get_float(elem);\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    if(!linalg_invert_matrix(data, o->m)) {\n",
    "        // TODO: I am not sure this is needed here. Otherwise, \n",
    "        // how should we free up the unused RAM of inverted?\n",
    "        m_del(mp_float_t, inverted->array->items, o->n*o->n);\n",
    "        mp_raise_ValueError(\"input matrix is singular\");\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(inverted);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_dot(mp_obj_t _m1, mp_obj_t _m2) {\n",
    "    // TODO: should the results be upcast?\n",
    "    ndarray_obj_t *m1 = MP_OBJ_TO_PTR(_m1);\n",
    "    ndarray_obj_t *m2 = MP_OBJ_TO_PTR(_m2);    \n",
    "    if(m1->n != m2->m) {\n",
    "        mp_raise_ValueError(\"matrix dimensions do not match\");\n",
    "    }\n",
    "    // TODO: numpy uses upcasting here\n",
    "    ndarray_obj_t *out = create_new_ndarray(m1->m, m2->n, NDARRAY_FLOAT);\n",
    "    mp_float_t *outdata = (mp_float_t *)out->array->items;\n",
    "    mp_float_t sum, v1, v2;\n",
    "    for(size_t i=0; i < m1->m; i++) { // rows of m1\n",
    "        for(size_t j=0; j < m2->n; j++) { // columns of m2\n",
    "            sum = 0.0;\n",
    "            for(size_t k=0; k < m2->m; k++) {\n",
    "                // (i, k) * (k, j)\n",
    "                v1 = ndarray_get_float_value(m1->array->items, m1->array->typecode, i*m1->n+k);\n",
    "                v2 = ndarray_get_float_value(m2->array->items, m2->array->typecode, k*m2->n+j);\n",
    "                sum += v1 * v2;\n",
    "            }\n",
    "            outdata[i*m1->m+j] = sum;\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_zeros_ones(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, uint8_t kind) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} } ,\n",
    "        { MP_QSTR_dtype, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = NDARRAY_FLOAT} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    uint8_t dtype = args[1].u_int;\n",
    "    if(!mp_obj_is_int(args[0].u_obj) && !mp_obj_is_type(args[0].u_obj, &mp_type_tuple)) {\n",
    "        mp_raise_TypeError(\"input argument must be an integer or a 2-tuple\");\n",
    "    }\n",
    "    ndarray_obj_t *ndarray = NULL;\n",
    "    if(mp_obj_is_int(args[0].u_obj)) {\n",
    "        size_t n = mp_obj_get_int(args[0].u_obj);\n",
    "        ndarray = create_new_ndarray(1, n, dtype);\n",
    "    } else if(mp_obj_is_type(args[0].u_obj, &mp_type_tuple)) {\n",
    "        mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(args[0].u_obj);\n",
    "        if(tuple->len != 2) {\n",
    "            mp_raise_TypeError(\"input argument must be an integer or a 2-tuple\");            \n",
    "        }\n",
    "        ndarray = create_new_ndarray(mp_obj_get_int(tuple->items[0]), \n",
    "                                                  mp_obj_get_int(tuple->items[1]), dtype);\n",
    "    }\n",
    "    if(kind == 1) {\n",
    "        mp_obj_t one = mp_obj_new_int(1);\n",
    "        for(size_t i=0; i < ndarray->array->len; i++) {\n",
    "            mp_binary_set_val_array(dtype, ndarray->array->items, i, one);\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(ndarray);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_zeros(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return linalg_zeros_ones(n_args, pos_args, kw_args, 0);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_ones(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return linalg_zeros_ones(n_args, pos_args, kw_args, 1);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_eye(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },\n",
    "        { MP_QSTR_M, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_k, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },        \n",
    "        { MP_QSTR_dtype, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = NDARRAY_FLOAT} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "\n",
    "    size_t n = args[0].u_int, m;\n",
    "    int16_t k = args[2].u_int;\n",
    "    uint8_t dtype = args[3].u_int;\n",
    "    if(args[1].u_rom_obj == mp_const_none) {\n",
    "        m = n;\n",
    "    } else {\n",
    "        m = mp_obj_get_int(args[1].u_rom_obj);\n",
    "    }\n",
    "    \n",
    "    ndarray_obj_t *ndarray = create_new_ndarray(m, n, dtype);\n",
    "    mp_obj_t one = mp_obj_new_int(1);\n",
    "    size_t i = 0;\n",
    "    if((k >= 0) && (k < n)) {\n",
    "        while(k < n) {\n",
    "            mp_binary_set_val_array(dtype, ndarray->array->items, i*n+k, one);\n",
    "            k++;\n",
    "            i++;\n",
    "        }\n",
    "    } else if((k < 0) && (-k < m)) {\n",
    "        k = -k;\n",
    "        i = 0;\n",
    "        while(k < m) {\n",
    "            mp_binary_set_val_array(dtype, ndarray->array->items, k*n+i, one);\n",
    "            k++;\n",
    "            i++;\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(ndarray);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_det(mp_obj_t oin) {\n",
    "    if(!mp_obj_is_type(oin, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"function defined for ndarrays only\");\n",
    "    }\n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(oin);\n",
    "    if(in->m != in->n) {\n",
    "        mp_raise_ValueError(\"input must be square matrix\");\n",
    "    }\n",
    "    \n",
    "    mp_float_t *tmp = m_new(mp_float_t, in->n*in->n);\n",
    "    for(size_t i=0; i < in->array->len; i++){\n",
    "        tmp[i] = ndarray_get_float_value(in->array->items, in->array->typecode, i);\n",
    "    }\n",
    "    mp_float_t c;\n",
    "    for(size_t m=0; m < in->m-1; m++){\n",
    "        if(MICROPY_FLOAT_C_FUN(fabs)(tmp[m*(in->n+1)]) < epsilon) {\n",
    "            m_del(mp_float_t, tmp, in->n*in->n);\n",
    "            return mp_obj_new_float(0.0);\n",
    "        }\n",
    "        for(size_t n=0; n < in->n; n++){\n",
    "            if(m != n) {\n",
    "                c = tmp[in->n*n+m] / tmp[m*(in->n+1)];\n",
    "                for(size_t k=0; k < in->n; k++){\n",
    "                    tmp[in->n*n+k] -= c * tmp[in->n*m+k];\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    mp_float_t det = 1.0;\n",
    "                            \n",
    "    for(size_t m=0; m < in->m; m++){ \n",
    "        det *= tmp[m*(in->n+1)];\n",
    "    }\n",
    "    m_del(mp_float_t, tmp, in->n*in->n);\n",
    "    return mp_obj_new_float(det);\n",
    "}\n",
    "\n",
    "mp_obj_t linalg_eig(mp_obj_t oin) {\n",
    "    if(!mp_obj_is_type(oin, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"function defined for ndarrays only\");\n",
    "    }\n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(oin);\n",
    "    if(in->m != in->n) {\n",
    "        mp_raise_ValueError(\"input must be square matrix\");\n",
    "    }\n",
    "    mp_float_t *array = m_new(mp_float_t, in->array->len);\n",
    "    for(size_t i=0; i < in->array->len; i++) {\n",
    "        array[i] = ndarray_get_float_value(in->array->items, in->array->typecode, i);\n",
    "    }\n",
    "    // make sure the matrix is symmetric\n",
    "    for(size_t m=0; m < in->m; m++) {\n",
    "        for(size_t n=m+1; n < in->n; n++) {\n",
    "            // compare entry (m, n) to (n, m)\n",
    "            // TODO: this must probably be scaled!\n",
    "            if(epsilon < MICROPY_FLOAT_C_FUN(fabs)(array[m*in->n + n] - array[n*in->n + m])) {\n",
    "                mp_raise_ValueError(\"input matrix is asymmetric\");\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    // if we got this far, then the matrix will be symmetric\n",
    "    \n",
    "    ndarray_obj_t *eigenvectors = create_new_ndarray(in->m, in->n, NDARRAY_FLOAT);\n",
    "    mp_float_t *eigvectors = (mp_float_t *)eigenvectors->array->items;\n",
    "    // start out with the unit matrix\n",
    "    for(size_t m=0; m < in->m; m++) {\n",
    "        eigvectors[m*(in->n+1)] = 1.0;\n",
    "    }\n",
    "    mp_float_t largest, w, t, c, s, tau, aMk, aNk, vm, vn;\n",
    "    size_t M, N;\n",
    "    size_t iterations = JACOBI_MAX*in->n*in->n;\n",
    "    do {\n",
    "        iterations--;\n",
    "        // find the pivot here\n",
    "        M = 0;\n",
    "        N = 0;\n",
    "        largest = 0.0;\n",
    "        for(size_t m=0; m < in->m-1; m++) { // -1: no need to inspect last row\n",
    "            for(size_t n=m+1; n < in->n; n++) {\n",
    "                w = MICROPY_FLOAT_C_FUN(fabs)(array[m*in->n + n]);\n",
    "                if((largest < w) && (epsilon < w)) {\n",
    "                    M = m;\n",
    "                    N = n;\n",
    "                    largest = w;\n",
    "                }\n",
    "            }\n",
    "        }\n",
    "        if(M+N == 0) { // all entries are smaller than epsilon, there is not much we can do...\n",
    "            break;\n",
    "        }\n",
    "        // at this point, we have the pivot, and it is the entry (M, N)\n",
    "        // now we have to find the rotation angle\n",
    "        w = (array[N*in->n + N] - array[M*in->n + M]) / (2.0*array[M*in->n + N]);\n",
    "        // The following if/else chooses the smaller absolute value for the tangent \n",
    "        // of the rotation angle. Going with the smaller should be numerically stabler.\n",
    "        if(w > 0) {\n",
    "            t = MICROPY_FLOAT_C_FUN(sqrt)(w*w + 1.0) - w;\n",
    "        } else {\n",
    "            t = -1.0*(MICROPY_FLOAT_C_FUN(sqrt)(w*w + 1.0) + w);\n",
    "        }\n",
    "        s = t / MICROPY_FLOAT_C_FUN(sqrt)(t*t + 1.0); // the sine of the rotation angle\n",
    "        c = 1.0 / MICROPY_FLOAT_C_FUN(sqrt)(t*t + 1.0); // the cosine of the rotation angle\n",
    "        tau = (1.0-c)/s; // this is equal to the tangent of the half of the rotation angle\n",
    "        \n",
    "        // at this point, we have the rotation angles, so we can transform the matrix\n",
    "        // first the two diagonal elements\n",
    "        // a(M, M) = a(M, M) - t*a(M, N)\n",
    "        array[M*in->n + M] = array[M*in->n + M] - t * array[M*in->n + N];\n",
    "        // a(N, N) = a(N, N) + t*a(M, N)\n",
    "        array[N*in->n + N] = array[N*in->n + N] + t * array[M*in->n + N];\n",
    "        // after the rotation, the a(M, N), and a(N, M) entries should become zero\n",
    "        array[M*in->n + N] = array[N*in->n + M] = 0.0;\n",
    "        // then all other elements in the column\n",
    "        for(size_t k=0; k < in->m; k++) {\n",
    "            if((k == M) || (k == N)) {\n",
    "                continue;\n",
    "            }\n",
    "            aMk = array[M*in->n + k];\n",
    "            aNk = array[N*in->n + k];\n",
    "            // a(M, k) = a(M, k) - s*(a(N, k) + tau*a(M, k))\n",
    "            array[M*in->n + k] -= s*(aNk + tau*aMk);\n",
    "            // a(N, k) = a(N, k) + s*(a(M, k) - tau*a(N, k))\n",
    "            array[N*in->n + k] += s*(aMk - tau*aNk);\n",
    "            // a(k, M) = a(M, k)\n",
    "            array[k*in->n + M] = array[M*in->n + k];\n",
    "            // a(k, N) = a(N, k)\n",
    "            array[k*in->n + N] = array[N*in->n + k];\n",
    "        }\n",
    "        // now we have to update the eigenvectors\n",
    "        // the rotation matrix, R, multiplies from the right\n",
    "        // R is the unit matrix, except for the \n",
    "        // R(M,M) = R(N, N) = c\n",
    "        // R(N, M) = s\n",
    "        // (M, N) = -s\n",
    "        // entries. This means that only the Mth, and Nth columns will change\n",
    "        for(size_t m=0; m < in->m; m++) {\n",
    "            vm = eigvectors[m*in->n+M];\n",
    "            vn = eigvectors[m*in->n+N];\n",
    "            // the new value of eigvectors(m, M)\n",
    "            eigvectors[m*in->n+M] = c * vm - s * vn;\n",
    "            // the new value of eigvectors(m, N)\n",
    "            eigvectors[m*in->n+N] = s * vm + c * vn;\n",
    "        }\n",
    "    } while(iterations > 0);\n",
    "    \n",
    "    if(iterations == 0) { \n",
    "        // the computation did not converge; numpy raises LinAlgError\n",
    "        m_del(mp_float_t, array, in->array->len);\n",
    "        mp_raise_ValueError(\"iterations did not converge\");\n",
    "    }\n",
    "    ndarray_obj_t *eigenvalues = create_new_ndarray(1, in->n, NDARRAY_FLOAT);\n",
    "    mp_float_t *eigvalues = (mp_float_t *)eigenvalues->array->items;\n",
    "    for(size_t i=0; i < in->n; i++) {\n",
    "        eigvalues[i] = array[i*(in->n+1)];\n",
    "    }\n",
    "    m_del(mp_float_t, array, in->array->len);\n",
    "    \n",
    "    mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(mp_obj_new_tuple(2, NULL));\n",
    "    tuple->items[0] = MP_OBJ_FROM_PTR(eigenvalues);\n",
    "    tuple->items[1] = MP_OBJ_FROM_PTR(eigenvectors);\n",
    "    return tuple;\n",
    "    return MP_OBJ_FROM_PTR(eigenvalues);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Vectorising mathematical operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## General comments"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "The following module implements the common mathematical functions for scalars, ndarrays (linear or matrix), and iterables. If the input argument is a scalar, a scalar is returned (i.e., for such arguments, these functions are identical to the functions in the `math` module), while for ndarrays, and iterables, the return value is an ndarray of type `float`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-18T19:02:39.320404Z",
     "start_time": "2019-09-18T19:02:39.300989Z"
    },
    "hidden": true,
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1D array:  ndarray([1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)\n",
      "\n",
      "exponent of an array (range(5)):  ndarray([1.0, 2.718281745910645, 7.389056205749512, 20.08553695678711, 54.59814834594727], dtype=float)\n",
      "\n",
      "exponent of a scalar (2.0):  7.38905609893065\n",
      "\n",
      " exponent of a 1D ndarray (a):  ndarray([2.718281745910645, 7.389056205749512, 20.08553695678711, 54.59814834594727, 148.4131622314453], dtype=float)\n",
      "\n",
      "2D matrix:  ndarray([[1.0, 2.0, 3.0],\n",
      "\t [4.0, 5.0, 6.0]], dtype=float)\n",
      "exponent of a 2D matrix (b):  ndarray([[2.718281745910645, 7.389056205749512, 20.08553695678711],\n",
      "\t [54.59814834594727, 148.4131622314453, 403.4288024902343]], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "# initialise an array\n",
    "a = ulab.ndarray([1, 2, 3, 4, 5])\n",
    "print('1D array: ', a)\n",
    "\n",
    "print('\\nexponent of an array (range(5)): ', ulab.exp(range(5)))\n",
    "\n",
    "print('\\nexponent of a scalar (2.0): ', ulab.exp(2.0))\n",
    "\n",
    "print('\\n exponent of a 1D ndarray (a): ', ulab.exp(a))\n",
    "\n",
    "# initialise a matrix\n",
    "b = ulab.ndarray([[1, 2, 3], [4, 5, 6]])\n",
    "print('\\n2D matrix: ', b)\n",
    "print('exponent of a 2D matrix (b): ', ulab.exp(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "Note that ndarrays are linear arrays in memory, even if the `shape` of the ndarray is a matrix. This means that we can treat both cases in a *single* loop."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "Since `ndarray`s are iterable, we could treat `ndarray`s, `list`s, `tuples`, and `range`s on the same footing. However, that would mean extra trips to a lot of functions, therefore, reading out the values of the `ndarray` directly is probably significantly faster. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## vectorise.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-29T06:58:41.845181Z",
     "start_time": "2019-11-29T06:58:41.839893Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 1478 bytes to vectorise.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode vectorise.h\n",
    "\n",
    "#ifndef _VECTORISE_\n",
    "#define _VECTORISE_\n",
    "\n",
    "#include \"ndarray.h\"\n",
    "\n",
    "mp_obj_t vectorise_acos(mp_obj_t );\n",
    "mp_obj_t vectorise_acosh(mp_obj_t );\n",
    "mp_obj_t vectorise_asin(mp_obj_t );\n",
    "mp_obj_t vectorise_asinh(mp_obj_t );\n",
    "mp_obj_t vectorise_atan(mp_obj_t );\n",
    "mp_obj_t vectorise_atanh(mp_obj_t );\n",
    "mp_obj_t vectorise_ceil(mp_obj_t );\n",
    "mp_obj_t vectorise_cos(mp_obj_t );\n",
    "mp_obj_t vectorise_erf(mp_obj_t );\n",
    "mp_obj_t vectorise_erfc(mp_obj_t );\n",
    "mp_obj_t vectorise_exp(mp_obj_t );\n",
    "mp_obj_t vectorise_expm1(mp_obj_t );\n",
    "mp_obj_t vectorise_floor(mp_obj_t );\n",
    "mp_obj_t vectorise_gamma(mp_obj_t );\n",
    "mp_obj_t vectorise_lgamma(mp_obj_t );\n",
    "mp_obj_t vectorise_log(mp_obj_t );\n",
    "mp_obj_t vectorise_log10(mp_obj_t );\n",
    "mp_obj_t vectorise_log2(mp_obj_t );\n",
    "mp_obj_t vectorise_sin(mp_obj_t );\n",
    "mp_obj_t vectorise_sinh(mp_obj_t );\n",
    "mp_obj_t vectorise_sqrt(mp_obj_t );\n",
    "mp_obj_t vectorise_tan(mp_obj_t );\n",
    "mp_obj_t vectorise_tanh(mp_obj_t );\n",
    "\n",
    "#define ITERATE_VECTOR(type, source, out) do {\\\n",
    "    type *input = (type *)(source)->array->items;\\\n",
    "    for(size_t i=0; i < (source)->array->len; i++) {\\\n",
    "                (out)[i] = f(input[i]);\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "#define MATH_FUN_1(py_name, c_name) \\\n",
    "    mp_obj_t vectorise_ ## py_name(mp_obj_t x_obj) { \\\n",
    "        return vectorise_generic_vector(x_obj, MICROPY_FLOAT_C_FUN(c_name)); \\\n",
    "    }\n",
    "    \n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## vectorise.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-06T16:15:07.570626Z",
     "start_time": "2019-11-06T16:15:07.564806Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 2878 bytes to vectorise.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode vectorise.c\n",
    "\n",
    "#include <math.h>\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/binary.h\"\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/objarray.h\"\n",
    "#include \"vectorise.h\"\n",
    "\n",
    "#ifndef MP_PI\n",
    "#define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)\n",
    "#endif\n",
    "    \n",
    "mp_obj_t vectorise_generic_vector(mp_obj_t o_in, mp_float_t (*f)(mp_float_t)) {\n",
    "    // Return a single value, if o_in is not iterable\n",
    "    if(mp_obj_is_float(o_in) || mp_obj_is_integer(o_in)) {\n",
    "            return mp_obj_new_float(f(mp_obj_get_float(o_in)));\n",
    "    }\n",
    "    mp_float_t x;\n",
    "    if(MP_OBJ_IS_TYPE(o_in, &ulab_ndarray_type)) {\n",
    "        ndarray_obj_t *source = MP_OBJ_TO_PTR(o_in);\n",
    "        ndarray_obj_t *ndarray = create_new_ndarray(source->m, source->n, NDARRAY_FLOAT);\n",
    "        mp_float_t *dataout = (mp_float_t *)ndarray->array->items;\n",
    "        if(source->array->typecode == NDARRAY_UINT8) {\n",
    "            ITERATE_VECTOR(uint8_t, source, dataout);\n",
    "        } else if(source->array->typecode == NDARRAY_INT8) {\n",
    "            ITERATE_VECTOR(int8_t, source, dataout);\n",
    "        } else if(source->array->typecode == NDARRAY_UINT16) {\n",
    "            ITERATE_VECTOR(uint16_t, source, dataout);\n",
    "        } else if(source->array->typecode == NDARRAY_INT16) {\n",
    "            ITERATE_VECTOR(int16_t, source, dataout);\n",
    "        } else {\n",
    "            ITERATE_VECTOR(mp_float_t, source, dataout);\n",
    "        }\n",
    "        return MP_OBJ_FROM_PTR(ndarray);\n",
    "    } else if(MP_OBJ_IS_TYPE(o_in, &mp_type_tuple) || MP_OBJ_IS_TYPE(o_in, &mp_type_list) || \n",
    "        MP_OBJ_IS_TYPE(o_in, &mp_type_range)) { // i.e., the input is a generic iterable\n",
    "            mp_obj_array_t *o = MP_OBJ_TO_PTR(o_in);\n",
    "            ndarray_obj_t *out = create_new_ndarray(1, o->len, NDARRAY_FLOAT);\n",
    "            mp_float_t *dataout = (mp_float_t *)out->array->items;\n",
    "            mp_obj_iter_buf_t iter_buf;\n",
    "            mp_obj_t item, iterable = mp_getiter(o_in, &iter_buf);\n",
    "            size_t i=0;\n",
    "            while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "                x = mp_obj_get_float(item);\n",
    "                dataout[i++] = f(x);\n",
    "            }\n",
    "        return MP_OBJ_FROM_PTR(out);\n",
    "    }\n",
    "    return mp_const_none;\n",
    "}\n",
    "\n",
    "MATH_FUN_1(acos, acos);\n",
    "MATH_FUN_1(acosh, acosh);\n",
    "MATH_FUN_1(asin, asin);\n",
    "MATH_FUN_1(asinh, asinh);\n",
    "MATH_FUN_1(atan, atan);\n",
    "MATH_FUN_1(atanh, atanh);\n",
    "MATH_FUN_1(ceil, ceil);\n",
    "MATH_FUN_1(cos, cos);\n",
    "MATH_FUN_1(erf, erf);\n",
    "MATH_FUN_1(erfc, erfc);\n",
    "MATH_FUN_1(exp, exp);\n",
    "MATH_FUN_1(expm1, expm1);\n",
    "MATH_FUN_1(floor, floor);\n",
    "MATH_FUN_1(gamma, tgamma);\n",
    "MATH_FUN_1(lgamma, lgamma);\n",
    "MATH_FUN_1(log, log);\n",
    "MATH_FUN_1(log10, log10);\n",
    "MATH_FUN_1(log2, log2);\n",
    "MATH_FUN_1(sin, sin);\n",
    "MATH_FUN_1(sinh, sinh);\n",
    "MATH_FUN_1(sqrt, sqrt);\n",
    "MATH_FUN_1(tan, tan);\n",
    "MATH_FUN_1(tanh, tanh);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Polynomials"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This module has two functions, `polyval`, and `polyfit`. The background for `polyfit` can be found under https://en.wikipedia.org/wiki/Polynomial_regression, and one can take the matrix inversion function from `linalg`. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Background \n",
    "\n",
    "An estimate, $\\beta$, for the coefficients of a polynomial fit can be gotten from\n",
    "\\begin{equation}\n",
    "\\vec{\\beta} = (\\mathbf{X}^T\\mathbf{X})^{-1}\\mathbf{X}^T \\vec{y}\n",
    "\\end{equation}\n",
    "where $\\vec{y}$ are the dependent values, and the matrix $X$ is constructed from the independent values as \n",
    "\\begin{equation}\n",
    "X = \\begin{pmatrix}\n",
    "1 & x_1^2 & x_1^2 & ... & x_1^m \n",
    "\\\\\n",
    "1 & x_2^2 & x_2^2 & ... & x_2^m \n",
    "\\\\\n",
    "\\vdots & \\vdots & \\vdots & \\ddots & \\vdots \n",
    "\\\\\n",
    "1 & x_n^2 & x_n^2 & ... & x_n^m \n",
    "\\end{pmatrix}\n",
    "\\end{equation}\n",
    "\n",
    "Note that the calculation of $X^T$ is trivial, and we need $X$ only once, namely in the product $X^TX$. We will save RAM by storing only $X^T$, and expressing $X$ from $X^T$, when we need it. The routine calculates the coefficients in increasing order, therefore, before returning, we have to reverse the array."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "### polyval"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 416,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-23T16:13:40.156166Z",
     "start_time": "2019-09-23T16:13:40.140712Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "coefficients:  [1, 1, 1, 0]\n",
      "independent values:  [0, 1, 2, 3, 4]\n",
      "\n",
      "values of p(x):  ndarray([0.0, 3.0, 14.0, 39.0, 84.0], dtype=float)\n",
      "\n",
      "ndarray (a):  ndarray([0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)\n",
      "value of p(a):  ndarray([0.0, 3.0, 14.0, 39.0, 84.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "p = [1, 1, 1, 0]\n",
    "x = [0, 1, 2, 3, 4]\n",
    "print('coefficients: ', p)\n",
    "print('independent values: ', x)\n",
    "print('\\nvalues of p(x): ', ulab.polyval(p, x))\n",
    "\n",
    "# the same works with ndarrays\n",
    "a = ulab.ndarray(x)\n",
    "print('\\nndarray (a): ', a)\n",
    "print('value of p(a): ', ulab.polyval(p, a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### polyfit\n",
    "\n",
    "First a perfect parabola with zero shift, and leading coefficient of 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 422,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-23T16:15:23.707678Z",
     "start_time": "2019-09-23T16:15:23.689198Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "independent values:  ndarray([-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], dtype=float)\n",
      "dependent values:  ndarray([9.0, 4.0, 1.0, 0.0, 1.0, 4.0, 9.0], dtype=float)\n",
      "fit values ndarray([1.00000011920929, 0.0, 0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "x = ulab.ndarray([-3, -2, -1, 0, 1, 2, 3])\n",
    "y = ulab.ndarray([9, 4, 1, 0, 1, 4, 9])\n",
    "print('independent values: ', x)\n",
    "print('dependent values: ', y)\n",
    "\n",
    "print('fit values', ulab.polyfit(x, y, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can now take a more meaningful example: the data points scatter here:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 423,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-23T16:15:27.832251Z",
     "start_time": "2019-09-23T16:15:27.813301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "independent values:  ndarray([-3.0, -2.0, -1.0, 0.0, 1.0, 2.0, 3.0], dtype=float)\n",
      "dependent values:  ndarray([10.0, 5.0, 1.0, 0.0, 1.0, 4.199999809265137, 9.100000381469727], dtype=float)\n",
      "fit values ndarray([1.065476179122925, -0.1535714119672775, 0.06666660308837891], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "x = ulab.ndarray([-3, -2, -1, 0, 1, 2, 3])\n",
    "y = ulab.ndarray([10, 5, 1, 0, 1, 4.2, 9.1])\n",
    "print('independent values: ', x)\n",
    "print('dependent values: ', y)\n",
    "\n",
    "print('fit values', ulab.polyfit(x, y, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Finally, let us see, what this looks like in numpy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 419,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-23T16:14:47.560695Z",
     "start_time": "2019-09-23T16:14:47.548914Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "independent values:  [-3 -2 -1  0  1  2  3]\n",
      "dependent values:  [10.   5.   1.   0.   1.   4.2  9.1]\n",
      "fit values:  [ 1.06547619 -0.15357143  0.06666667]\n"
     ]
    }
   ],
   "source": [
    "x = array([-3, -2, -1, 0, 1, 2, 3])\n",
    "y = array([10, 5, 1, 0, 1, 4.2, 9.1])\n",
    "print('independent values: ', x)\n",
    "print('dependent values: ', y)\n",
    "\n",
    "print('fit values: ', polyfit(x, y, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Look at that! The difference to numpy is minuscule!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## poly.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-21T05:29:23.021150Z",
     "start_time": "2019-09-21T05:29:23.014023Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 315 bytes to poly.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode poly.h\n",
    "\n",
    "#ifndef _POLY_\n",
    "#define _POLY_\n",
    "\n",
    "mp_obj_t poly_polyval(mp_obj_t , mp_obj_t );\n",
    "mp_obj_t poly_polyfit(size_t  , const mp_obj_t *);\n",
    "\n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## poly.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 346,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-01T12:09:15.148115Z",
     "start_time": "2019-11-01T12:09:15.073086Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 6859 bytes to poly.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode poly.c\n",
    "\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/objarray.h\"\n",
    "#include \"ndarray.h\"\n",
    "#include \"linalg.h\"\n",
    "#include \"poly.h\"\n",
    "\n",
    "\n",
    "bool object_is_nditerable(mp_obj_t o_in) {\n",
    "    if(mp_obj_is_type(o_in, &ulab_ndarray_type) || \n",
    "      mp_obj_is_type(o_in, &mp_type_tuple) || \n",
    "      mp_obj_is_type(o_in, &mp_type_list) || \n",
    "      mp_obj_is_type(o_in, &mp_type_range)) {\n",
    "        return true;\n",
    "    }\n",
    "    return false;\n",
    "}\n",
    "\n",
    "size_t get_nditerable_len(mp_obj_t o_in) {\n",
    "    if(mp_obj_is_type(o_in, &ulab_ndarray_type)) {\n",
    "        ndarray_obj_t *in = MP_OBJ_TO_PTR(o_in);\n",
    "        return in->array->len;\n",
    "    } else {\n",
    "        return (size_t)mp_obj_get_int(mp_obj_len_maybe(o_in));\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t poly_polyval(mp_obj_t o_p, mp_obj_t o_x) {\n",
    "    // TODO: return immediately, if o_p is not an iterable\n",
    "    // TODO: there is a bug here: matrices won't work, \n",
    "    // because there is a single iteration loop\n",
    "    size_t m, n;\n",
    "    if(MP_OBJ_IS_TYPE(o_x, &ulab_ndarray_type)) {\n",
    "        ndarray_obj_t *ndx = MP_OBJ_TO_PTR(o_x);\n",
    "        m = ndx->m;\n",
    "        n = ndx->n;\n",
    "    } else {\n",
    "        mp_obj_array_t *ix = MP_OBJ_TO_PTR(o_x);\n",
    "        m = 1;\n",
    "        n = ix->len;\n",
    "    }\n",
    "    // polynomials are going to be of type float, except, when both \n",
    "    // the coefficients and the independent variable are integers\n",
    "    ndarray_obj_t *out = create_new_ndarray(m, n, NDARRAY_FLOAT);\n",
    "    mp_obj_iter_buf_t x_buf;\n",
    "    mp_obj_t x_item, x_iterable = mp_getiter(o_x, &x_buf);\n",
    "\n",
    "    mp_obj_iter_buf_t p_buf;\n",
    "    mp_obj_t p_item, p_iterable;\n",
    "\n",
    "    mp_float_t x, y;\n",
    "    mp_float_t *outf = (mp_float_t *)out->array->items;\n",
    "    uint8_t plen = mp_obj_get_int(mp_obj_len_maybe(o_p));\n",
    "    mp_float_t *p = m_new(mp_float_t, plen);\n",
    "    p_iterable = mp_getiter(o_p, &p_buf);\n",
    "    uint16_t i = 0;    \n",
    "    while((p_item = mp_iternext(p_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        p[i] = mp_obj_get_float(p_item);\n",
    "        i++;\n",
    "    }\n",
    "    i = 0;\n",
    "    while ((x_item = mp_iternext(x_iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        x = mp_obj_get_float(x_item);\n",
    "        y = p[0];\n",
    "        for(uint8_t j=0; j < plen-1; j++) {\n",
    "            y *= x;\n",
    "            y += p[j+1];\n",
    "        }\n",
    "        outf[i++] = y;\n",
    "    }\n",
    "    m_del(mp_float_t, p, plen);\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "}\n",
    "\n",
    "mp_obj_t poly_polyfit(size_t  n_args, const mp_obj_t *args) {\n",
    "    if((n_args != 2) && (n_args != 3)) {\n",
    "        mp_raise_ValueError(\"number of arguments must be 2, or 3\");\n",
    "    }\n",
    "    if(!object_is_nditerable(args[0])) {\n",
    "        mp_raise_ValueError(\"input data must be an iterable\");\n",
    "    }\n",
    "    uint16_t lenx, leny;\n",
    "    uint8_t deg;\n",
    "    mp_float_t *x, *XT, *y, *prod;\n",
    "\n",
    "    if(n_args == 2) { // only the y values are supplied\n",
    "        // TODO: this is actually not enough: the first argument can very well be a matrix, \n",
    "        // in which case we are between the rock and a hard place\n",
    "        leny = (uint16_t)mp_obj_get_int(mp_obj_len_maybe(args[0]));\n",
    "        deg = (uint8_t)mp_obj_get_int(args[1]);\n",
    "        if(leny < deg) {\n",
    "            mp_raise_ValueError(\"more degrees of freedom than data points\");\n",
    "        }\n",
    "        lenx = leny;\n",
    "        x = m_new(mp_float_t, lenx); // assume uniformly spaced data points\n",
    "        for(size_t i=0; i < lenx; i++) {\n",
    "            x[i] = i;\n",
    "        }\n",
    "        y = m_new(mp_float_t, leny);\n",
    "        fill_array_iterable(y, args[0]);\n",
    "    } else if(n_args == 3) {\n",
    "        lenx = (uint16_t)mp_obj_get_int(mp_obj_len_maybe(args[0]));\n",
    "        leny = (uint16_t)mp_obj_get_int(mp_obj_len_maybe(args[0]));\n",
    "        if(lenx != leny) {\n",
    "            mp_raise_ValueError(\"input vectors must be of equal length\");\n",
    "        }\n",
    "        deg = (uint8_t)mp_obj_get_int(args[2]);\n",
    "        if(leny < deg) {\n",
    "            mp_raise_ValueError(\"more degrees of freedom than data points\");\n",
    "        }\n",
    "        x = m_new(mp_float_t, lenx);\n",
    "        fill_array_iterable(x, args[0]);\n",
    "        y = m_new(mp_float_t, leny);\n",
    "        fill_array_iterable(y, args[1]);\n",
    "    }\n",
    "    \n",
    "    // one could probably express X as a function of XT, \n",
    "    // and thereby save RAM, because X is used only in the product\n",
    "    XT = m_new(mp_float_t, (deg+1)*leny); // XT is a matrix of shape (deg+1, len) (rows, columns)\n",
    "    for(uint8_t i=0; i < leny; i++) { // column index\n",
    "        XT[i+0*lenx] = 1.0; // top row\n",
    "        for(uint8_t j=1; j < deg+1; j++) { // row index\n",
    "            XT[i+j*leny] = XT[i+(j-1)*leny]*x[i];\n",
    "        }\n",
    "    }\n",
    "    \n",
    "    prod = m_new(mp_float_t, (deg+1)*(deg+1)); // the product matrix is of shape (deg+1, deg+1)\n",
    "    mp_float_t sum;\n",
    "    for(uint16_t i=0; i < deg+1; i++) { // column index\n",
    "        for(uint16_t j=0; j < deg+1; j++) { // row index\n",
    "            sum = 0.0;\n",
    "            for(size_t k=0; k < lenx; k++) {\n",
    "                // (j, k) * (k, i) \n",
    "                // Note that the second matrix is simply the transpose of the first: \n",
    "                // X(k, i) = XT(i, k) = XT[k*lenx+i]\n",
    "                sum += XT[j*lenx+k]*XT[i*lenx+k]; // X[k*(deg+1)+i];\n",
    "            }\n",
    "            prod[j*(deg+1)+i] = sum;\n",
    "        }\n",
    "    }\n",
    "    if(!linalg_invert_matrix(prod, deg+1)) {\n",
    "        // Although X was a Vandermonde matrix, whose inverse is guaranteed to exist, \n",
    "        // we bail out here, if prod couldn't be inverted: if the values in x are not all \n",
    "        // distinct, prod is singular\n",
    "        m_del(mp_float_t, XT, (deg+1)*lenx);\n",
    "        m_del(mp_float_t, x, lenx);\n",
    "        m_del(mp_float_t, y, lenx);\n",
    "        m_del(mp_float_t, prod, (deg+1)*(deg+1));\n",
    "        mp_raise_ValueError(\"could not invert Vandermonde matrix\");\n",
    "    } \n",
    "    // at this point, we have the inverse of X^T * X\n",
    "    // y is a column vector; x is free now, we can use it for storing intermediate values\n",
    "    for(uint16_t i=0; i < deg+1; i++) { // row index\n",
    "        sum = 0.0;\n",
    "        for(uint16_t j=0; j < lenx; j++) { // column index\n",
    "            sum += XT[i*lenx+j]*y[j];\n",
    "        }\n",
    "        x[i] = sum;\n",
    "    }\n",
    "    // XT is no longer needed\n",
    "    m_del(mp_float_t, XT, (deg+1)*leny);\n",
    "    \n",
    "    ndarray_obj_t *beta = create_new_ndarray(deg+1, 1, NDARRAY_FLOAT);\n",
    "    mp_float_t *betav = (mp_float_t *)beta->array->items;\n",
    "    // x[0..(deg+1)] contains now the product X^T * y; we can get rid of y\n",
    "    m_del(float, y, leny);\n",
    "    \n",
    "    // now, we calculate beta, i.e., we apply prod = (X^T * X)^(-1) on x = X^T * y; x is a column vector now\n",
    "    for(uint16_t i=0; i < deg+1; i++) {\n",
    "        sum = 0.0;\n",
    "        for(uint16_t j=0; j < deg+1; j++) {\n",
    "            sum += prod[i*(deg+1)+j]*x[j];\n",
    "        }\n",
    "        betav[i] = sum;\n",
    "    }\n",
    "    m_del(mp_float_t, x, lenx);\n",
    "    m_del(mp_float_t, prod, (deg+1)*(deg+1));\n",
    "    for(uint8_t i=0; i < (deg+1)/2; i++) {\n",
    "        // We have to reverse the array, for the leading coefficient comes first. \n",
    "        SWAP(mp_float_t, betav[i], betav[deg-i]);\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(beta);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fast Fourier transform"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The original idea of the implementation of the fast Fourier transform is taken from Numerical recipes. The main modification is that the present FFT kernel requires two input vectors of float type: one for the real part, and one for the imaginary part, while in Numerical recipes, the real and imaginary parts occupy alternating positions in the same array. \n",
    "\n",
    "However, since `ndarray` cannot hold complex types, it makes sense to starts with two separate vectors. This is especially true for our particular case, since the data are most probably real, coming from an ADC or similar. By separating the real and imaginary parts at the very beginning, we can process *real* data by not providing the imaginary part. If only one argument is supplied, it is assumed to be real, and the imaginary part is automatically filled in.\n",
    "\n",
    "Now, the implementation computes the transform in place. This means that RAM space could be saved, if the old data are not required anymore. The problem, however, is that the results are of type float, irrespective of the input type. If one can somehow guarantee that the input type is also float, then the old data can be overwritten. This is what happens in the `spectrum` function that overwrites the input array."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Full FFT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 435,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-24T05:45:27.502164Z",
     "start_time": "2019-09-24T05:45:27.481684Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "real part:  ndarray([12.0, 0.0, -3.999999761581421, 0.0, -4.0, 0.0, -4.0, 0.0], dtype=float)\n",
      "imag part:  ndarray([0.0, 0.0, 3.999999523162842, 0.0, 0.0, 0.0, -3.999999523162842, 0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([0, 1, 2, 3, 0, 1, 2, 3])\n",
    "re, im = ulab.fft(a)\n",
    "print('real part: ', re)\n",
    "print('imag part: ', im)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "The same Fourier transform on numpy:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 436,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-24T05:45:47.456871Z",
     "start_time": "2019-09-24T05:45:47.413636Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12.+0.j,  0.+0.j, -4.+4.j,  0.+0.j, -4.+0.j,  0.+0.j, -4.-4.j,\n",
       "        0.+0.j])"
      ]
     },
     "execution_count": 436,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fft.fft([0, 1, 2, 3, 0, 1, 2, 3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true,
    "hidden": true
   },
   "source": [
    "### Spectrum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 447,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-24T15:48:10.433996Z",
     "start_time": "2019-09-24T15:48:10.416544Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([12.0, 0.0, 5.656853675842285, 0.0, 4.0, 0.0, 5.656853675842285, 0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([0, 1, 2, 3, 0, 1, 2, 3])\n",
    "ulab.spectrum(a)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "hidden": true
   },
   "source": [
    "And watch this: if you need the spectrum, but do not want to overwrite your data, you can do the following"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 440,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-09-24T06:02:23.773274Z",
     "start_time": "2019-09-24T06:02:23.668291Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "spectrum:  ndarray([12.0, 0.0, 5.656853675842285, 0.0, 4.0, 0.0, 5.656853675842285, 0.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([0, 1, 2, 3, 0, 1, 2, 3])\n",
    "re, im = ulab.fft(a)\n",
    "print('spectrum: ', ulab.sqrt(re*re+im*im))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fft.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1672,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-17T05:33:40.184963Z",
     "start_time": "2019-10-17T05:33:40.177050Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 491 bytes to fft.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode fft.h\n",
    "\n",
    "#ifndef _FFT_\n",
    "#define _FFT_\n",
    "\n",
    "#ifndef MP_PI\n",
    "#define MP_PI MICROPY_FLOAT_CONST(3.14159265358979323846)\n",
    "#endif\n",
    "\n",
    "#define SWAP(t, a, b) { t tmp = a; a = b; b = tmp; }\n",
    "\n",
    "mp_obj_t fft_fft(size_t , const mp_obj_t *);\n",
    "mp_obj_t fft_ifft(size_t , const mp_obj_t *);\n",
    "mp_obj_t fft_spectrum(size_t , const mp_obj_t *);\n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## fft.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-06T16:28:08.210625Z",
     "start_time": "2019-11-06T16:28:08.205117Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 5401 bytes to fft.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode fft.c\n",
    "\n",
    "#include <math.h>\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include <string.h>\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/binary.h\"\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/objarray.h\"\n",
    "#include \"ndarray.h\"\n",
    "#include \"fft.h\"\n",
    "\n",
    "enum FFT_TYPE {\n",
    "    FFT_FFT,\n",
    "    FFT_IFFT,\n",
    "    FFT_SPECTRUM,\n",
    "};\n",
    "\n",
    "void fft_kernel(mp_float_t *real, mp_float_t *imag, int n, int isign) {\n",
    "    // This is basically a modification of four1 from Numerical Recipes\n",
    "    // The main difference is that this function takes two arrays, one \n",
    "    // for the real, and one for the imaginary parts. \n",
    "    int j, m, mmax, istep;\n",
    "    mp_float_t tempr, tempi;\n",
    "    mp_float_t wtemp, wr, wpr, wpi, wi, theta;\n",
    "\n",
    "    j = 0;\n",
    "    for(int i = 0; i < n; i++) {\n",
    "        if (j > i) {\n",
    "            SWAP(mp_float_t, real[i], real[j]);\n",
    "            SWAP(mp_float_t, imag[i], imag[j]);\n",
    "        }\n",
    "        m = n >> 1;\n",
    "        while (j >= m && m > 0) {\n",
    "            j -= m;\n",
    "            m >>= 1;\n",
    "        }\n",
    "        j += m;\n",
    "    }\n",
    "\n",
    "    mmax = 1;\n",
    "    while (n > mmax) {\n",
    "        istep = mmax << 1;\n",
    "        theta = -2.0*isign*MP_PI/istep;\n",
    "        wtemp = MICROPY_FLOAT_C_FUN(sin)(0.5 * theta);\n",
    "        wpr = -2.0 * wtemp * wtemp;\n",
    "        wpi = MICROPY_FLOAT_C_FUN(sin)(theta);\n",
    "        wr = 1.0;\n",
    "        wi = 0.0;\n",
    "        for(m = 0; m < mmax; m++) {\n",
    "            for(int i = m; i < n; i += istep) {\n",
    "                j = i + mmax;\n",
    "                tempr = wr * real[j] - wi * imag[j];\n",
    "                tempi = wr * imag[j] + wi * real[j];\n",
    "                real[j] = real[i] - tempr;\n",
    "                imag[j] = imag[i] - tempi;\n",
    "                real[i] += tempr;\n",
    "                imag[i] += tempi;\n",
    "            }\n",
    "            wtemp = wr;\n",
    "            wr = wr*wpr - wi*wpi + wr;\n",
    "            wi = wi*wpr + wtemp*wpi + wi;\n",
    "        }\n",
    "        mmax = istep;\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t fft_fft_ifft_spectrum(size_t n_args, mp_obj_t arg_re, mp_obj_t arg_im, uint8_t type) {\n",
    "    if(!MP_OBJ_IS_TYPE(arg_re, &ulab_ndarray_type)) {\n",
    "        mp_raise_NotImplementedError(\"FFT is defined for ndarrays only\");\n",
    "    } \n",
    "    if(n_args == 2) {\n",
    "        if(!MP_OBJ_IS_TYPE(arg_im, &ulab_ndarray_type)) {\n",
    "            mp_raise_NotImplementedError(\"FFT is defined for ndarrays only\");\n",
    "        }\n",
    "    }\n",
    "    // Check if input is of length of power of 2\n",
    "    ndarray_obj_t *re = MP_OBJ_TO_PTR(arg_re);\n",
    "    uint16_t len = re->array->len;\n",
    "    if((len & (len-1)) != 0) {\n",
    "        mp_raise_ValueError(\"input array length must be power of 2\");\n",
    "    }\n",
    "    \n",
    "    ndarray_obj_t *out_re = create_new_ndarray(1, len, NDARRAY_FLOAT);\n",
    "    mp_float_t *data_re = (mp_float_t *)out_re->array->items;\n",
    "    \n",
    "    if(re->array->typecode == NDARRAY_FLOAT) { \n",
    "        // By treating this case separately, we can save a bit of time.\n",
    "        // I don't know if it is worthwhile, though...\n",
    "        memcpy((mp_float_t *)out_re->array->items, (mp_float_t *)re->array->items, re->bytes);\n",
    "    } else {\n",
    "        for(size_t i=0; i < len; i++) {\n",
    "            data_re[i] = ndarray_get_float_value(re->array->items, re->array->typecode, i);\n",
    "        }\n",
    "    }\n",
    "    ndarray_obj_t *out_im = create_new_ndarray(1, len, NDARRAY_FLOAT);\n",
    "    mp_float_t *data_im = (mp_float_t *)out_im->array->items;\n",
    "\n",
    "    if(n_args == 2) {\n",
    "        ndarray_obj_t *im = MP_OBJ_TO_PTR(arg_im);\n",
    "        if (re->array->len != im->array->len) {\n",
    "            mp_raise_ValueError(\"real and imaginary parts must be of equal length\");\n",
    "        }\n",
    "        if(im->array->typecode == NDARRAY_FLOAT) {\n",
    "            memcpy((mp_float_t *)out_im->array->items, (mp_float_t *)im->array->items, im->bytes);\n",
    "        } else {\n",
    "            for(size_t i=0; i < len; i++) {\n",
    "                data_im[i] = ndarray_get_float_value(im->array->items, im->array->typecode, i);\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    if((type == FFT_FFT) || (type == FFT_SPECTRUM)) {\n",
    "        fft_kernel(data_re, data_im, len, 1);\n",
    "        if(type == FFT_SPECTRUM) {\n",
    "            for(size_t i=0; i < len; i++) {\n",
    "                data_re[i] = MICROPY_FLOAT_C_FUN(sqrt)(data_re[i]*data_re[i] + data_im[i]*data_im[i]);\n",
    "            }\n",
    "        }\n",
    "    } else { // inverse transform\n",
    "        fft_kernel(data_re, data_im, len, -1);\n",
    "        // TODO: numpy accepts the norm keyword argument\n",
    "        for(size_t i=0; i < len; i++) {\n",
    "            data_re[i] /= len;\n",
    "            data_im[i] /= len;\n",
    "        }\n",
    "    }\n",
    "    if(type == FFT_SPECTRUM) {\n",
    "        return MP_OBJ_TO_PTR(out_re);\n",
    "    } else {\n",
    "        mp_obj_t tuple[2];\n",
    "        tuple[0] = out_re;\n",
    "        tuple[1] = out_im;\n",
    "        return mp_obj_new_tuple(2, tuple);\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t fft_fft(size_t n_args, const mp_obj_t *args) {\n",
    "    if(n_args == 2) {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_FFT);\n",
    "    } else {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], mp_const_none, FFT_FFT);        \n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t fft_ifft(size_t n_args, const mp_obj_t *args) {\n",
    "    if(n_args == 2) {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_IFFT);\n",
    "    } else {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], mp_const_none, FFT_IFFT);\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t fft_spectrum(size_t n_args, const mp_obj_t *args) {\n",
    "    if(n_args == 2) {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], args[1], FFT_SPECTRUM);\n",
    "    } else {\n",
    "        return fft_fft_ifft_spectrum(n_args, args[0], mp_const_none, FFT_SPECTRUM);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Numerical\n",
    "\n",
    "## General comments\n",
    "\n",
    "This section contains miscellaneous functions that did not fit in the other submodules. These include `linspace`, `min/max`, `argmin/argmax`, `sum`, `mean`, `std`. These latter functions work with iterables, or ndarrays. When the ndarray is two-dimensional, an `axis` keyword can be supplied, in which case, the function returns a vector, otherwise a scalar.\n",
    "\n",
    "Since the return values of `mean`, and `std` are most probably floats, these functions return ndarrays of type float, while `min/max` and `clip` do not change the type, and `argmin/argmax` return `uint8`, if the values are smaller than 255, otherwise, `uint16`.\n",
    "\n",
    "### roll\n",
    "\n",
    "Note that at present, arrays are always rolled to the left, even when the user specifies right. The reason for that is inner working of `memcpy`: one can shift contiguous chunks to the left only. If one tries to shift to the right, then the same value will be written into the new array over and over again."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "heading_collapsed": true
   },
   "source": [
    "## Examples"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-03T13:18:25.918459Z",
     "start_time": "2019-10-03T13:18:25.902846Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([0.0, 1.0, 2.0, ..., 8.0, 9.0, 10.0], dtype=float)\n",
      "6.0\n",
      "ndarray([[1, 2, 3, 4, 5],\n",
      "\t [6, 7, 8, 9, 10],\n",
      "\t [55, 66, 77, 88, 99]], dtype=int8)\n",
      "ndarray([[55, 66, 77, 88, 99],\n",
      "\t [1, 2, 3, 4, 5],\n",
      "\t [6, 7, 8, 9, 10]], dtype=int8)\n",
      "ndarray([[66, 77, 88, 99, 55],\n",
      "\t [2, 3, 4, 5, 1],\n",
      "\t [7, 8, 9, 10, 6]], dtype=int8)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "print(ulab.linspace(0, 10, 11))\n",
    "print(ulab.sum([1, 2, 3]))\n",
    "\n",
    "a = ulab.ndarray([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [55, 66, 77, 88, 99]], dtype=ulab.int8)\n",
    "print(a)\n",
    "ulab.roll(a, -1, axis=0)\n",
    "print(a)\n",
    "ulab.roll(a, 1, axis=1)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 819,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T18:43:41.234563Z",
     "start_time": "2019-10-07T18:43:41.226790Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int8), 0.9090909090909091)"
      ]
     },
     "execution_count": 819,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "linspace(0, 10, 11, endpoint=False, dtype=int8, retstep=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 863,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T04:57:13.952836Z",
     "start_time": "2019-10-08T04:57:13.941799Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([0., 1., 2., 3.]), array([-0., -1., -2., -3.]))"
      ]
     },
     "execution_count": 863,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = array([0, 1, 2, 3], dtype=float)\n",
    "abs(a), -a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 834,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-07T18:47:13.743285Z",
     "start_time": "2019-10-07T18:47:13.720493Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(ndarray([0, 1, 2, ..., 8, 9, 10], dtype=int8), 1.0)\n",
      "(ndarray([0, 0, 1, ..., 7, 8, 9], dtype=int16), 0.9090909361839294)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "print(ulab.linspace(0, 10, num=11, endpoint=True, retstep=True, dtype=ulab.int8))\n",
    "print(ulab.linspace(0, 10, num=11, endpoint=False, retstep=True, dtype=ulab.int16))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 971,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-10-08T15:10:19.283419Z",
     "start_time": "2019-10-08T15:10:19.259337Z"
    },
    "hidden": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ndarray([0.0, 1.0, 2.0, 3.0], dtype=float)\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "%%micropython -unix 1\n",
    "\n",
    "import ulab\n",
    "\n",
    "a = ulab.ndarray([0, 1, 2, -3], dtype=ulab.float)\n",
    "print(abs(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numerical.h"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-06T06:20:29.556126Z",
     "start_time": "2019-11-06T06:20:29.547543Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 4779 bytes to numerical.h\n"
     ]
    }
   ],
   "source": [
    "%%ccode numerical.h\n",
    "\n",
    "#ifndef _NUMERICAL_\n",
    "#define _NUMERICAL_\n",
    "\n",
    "#include \"ndarray.h\"\n",
    "\n",
    "mp_obj_t numerical_linspace(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_sum(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_mean(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_std(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_min(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_max(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_argmin(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_argmax(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_roll(size_t , const mp_obj_t *, mp_map_t *);\n",
    "\n",
    "// TODO: implement minimum/maximum, and cumsum\n",
    "mp_obj_t numerical_minimum(mp_obj_t , mp_obj_t );\n",
    "mp_obj_t numerical_maximum(mp_obj_t , mp_obj_t );\n",
    "mp_obj_t numerical_cumsum(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_flip(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_diff(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_sort(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_sort_inplace(size_t , const mp_obj_t *, mp_map_t *);\n",
    "mp_obj_t numerical_argsort(size_t , const mp_obj_t *, mp_map_t *);\n",
    "\n",
    "// this macro could be tighter, if we moved the ifs to the argmin function, assigned <, as well as >\n",
    "#define ARG_MIN_LOOP(in, type, start, stop, stride, op) do {\\\n",
    "    type *array = (type *)(in)->array->items;\\\n",
    "    if(((op) == NUMERICAL_MAX) || ((op) == NUMERICAL_ARGMAX)) {\\\n",
    "        for(size_t i=(start)+(stride); i < (stop); i+=(stride)) {\\\n",
    "            if((array)[i] > (array)[best_idx]) {\\\n",
    "                best_idx = i;\\\n",
    "            }\\\n",
    "        }\\\n",
    "    } else{\\\n",
    "        for(size_t i=(start)+(stride); i < (stop); i+=(stride)) {\\\n",
    "            if((array)[i] < (array)[best_idx]) best_idx = i;\\\n",
    "        }\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "#define CALCULATE_DIFF(in, out, type, M, N, inn, increment) do {\\\n",
    "    type *source = (type *)(in)->array->items;\\\n",
    "    type *target = (type *)(out)->array->items;\\\n",
    "    for(size_t i=0; i < (M); i++) {\\\n",
    "        for(size_t j=0; j < (N); j++) {\\\n",
    "            for(uint8_t k=0; k < n+1; k++) {\\\n",
    "                target[i*(N)+j] -= stencil[k]*source[i*(inn)+j+k*(increment)];\\\n",
    "            }\\\n",
    "        }\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "#define HEAPSORT(type, ndarray) do {\\\n",
    "    type *array = (type *)(ndarray)->array->items;\\\n",
    "    type tmp;\\\n",
    "    for (;;) {\\\n",
    "        if (k > 0) {\\\n",
    "            tmp = array[start+(--k)*increment];\\\n",
    "        } else {\\\n",
    "            q--;\\\n",
    "            if(q == 0) {\\\n",
    "                break;\\\n",
    "            }\\\n",
    "            tmp = array[start+q*increment];\\\n",
    "            array[start+q*increment] = array[start];\\\n",
    "        }\\\n",
    "        p = k;\\\n",
    "        c = k + k + 1;\\\n",
    "        while (c < q) {\\\n",
    "            if((c + 1 < q)  &&  (array[start+(c+1)*increment] > array[start+c*increment])) {\\\n",
    "                c++;\\\n",
    "            }\\\n",
    "            if(array[start+c*increment] > tmp) {\\\n",
    "                array[start+p*increment] = array[start+c*increment];\\\n",
    "                p = c;\\\n",
    "                c = p + p + 1;\\\n",
    "            } else {\\\n",
    "                break;\\\n",
    "            }\\\n",
    "        }\\\n",
    "        array[start+p*increment] = tmp;\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "// This is pretty similar to HEAPSORT above; perhaps, the two could be combined somehow\n",
    "// On the other hand, since this is a macro, it doesn't really matter\n",
    "// Keep in mind that initially, index_array[start+s*increment] = s\n",
    "#define HEAP_ARGSORT(type, ndarray, index_array) do {\\\n",
    "    type *array = (type *)(ndarray)->array->items;\\\n",
    "    type tmp;\\\n",
    "    uint16_t itmp;\\\n",
    "    for (;;) {\\\n",
    "        if (k > 0) {\\\n",
    "            k--;\\\n",
    "            tmp = array[start+index_array[start+k*increment]*increment];\\\n",
    "            itmp = index_array[start+k*increment];\\\n",
    "        } else {\\\n",
    "            q--;\\\n",
    "            if(q == 0) {\\\n",
    "                break;\\\n",
    "            }\\\n",
    "            tmp = array[start+index_array[start+q*increment]*increment];\\\n",
    "            itmp = index_array[start+q*increment];\\\n",
    "            index_array[start+q*increment] = index_array[start];\\\n",
    "        }\\\n",
    "        p = k;\\\n",
    "        c = k + k + 1;\\\n",
    "        while (c < q) {\\\n",
    "            if((c + 1 < q)  &&  (array[start+index_array[start+(c+1)*increment]*increment] > array[start+index_array[start+c*increment]*increment])) {\\\n",
    "                c++;\\\n",
    "            }\\\n",
    "            if(array[start+index_array[start+c*increment]*increment] > tmp) {\\\n",
    "                index_array[start+p*increment] = index_array[start+c*increment];\\\n",
    "                p = c;\\\n",
    "                c = p + p + 1;\\\n",
    "            } else {\\\n",
    "                break;\\\n",
    "            }\\\n",
    "        }\\\n",
    "        index_array[start+p*increment] = itmp;\\\n",
    "    }\\\n",
    "} while(0)\n",
    "\n",
    "#endif"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## numerical.c\n",
    "\n",
    "### Argument parsing\n",
    "\n",
    "Since most of these functions operate on matrices along an axis, it might make sense to factor out the parsing of arguments and keyword arguments. The void function `numerical_parse_args` fills in the pointer for the matrix/array, and the axis."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-06T16:29:58.539854Z",
     "start_time": "2019-11-06T16:29:58.528718Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 28599 bytes to numerical.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode numerical.c\n",
    "\n",
    "#include <math.h>\n",
    "#include <stdlib.h>\n",
    "#include <string.h>\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/objint.h\"\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/builtin.h\"\n",
    "#include \"py/misc.h\"\n",
    "#include \"numerical.h\"\n",
    "\n",
    "enum NUMERICAL_FUNCTION_TYPE {\n",
    "    NUMERICAL_MIN,\n",
    "    NUMERICAL_MAX,\n",
    "    NUMERICAL_ARGMIN,\n",
    "    NUMERICAL_ARGMAX,\n",
    "    NUMERICAL_SUM,\n",
    "    NUMERICAL_MEAN,\n",
    "    NUMERICAL_STD,\n",
    "};\n",
    "\n",
    "mp_obj_t numerical_linspace(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_num, MP_ARG_INT, {.u_int = 50} },\n",
    "        { MP_QSTR_endpoint, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_true_obj)} },\n",
    "        { MP_QSTR_retstep, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_false_obj)} },\n",
    "        { MP_QSTR_dtype, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = NDARRAY_FLOAT} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(2, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "\n",
    "    uint16_t len = args[2].u_int;\n",
    "    if(len < 2) {\n",
    "        mp_raise_ValueError(\"number of points must be at least 2\");\n",
    "    }\n",
    "    mp_float_t value, step;\n",
    "    value = mp_obj_get_float(args[0].u_obj);\n",
    "    uint8_t typecode = args[5].u_int;\n",
    "    if(args[3].u_obj == mp_const_true) step = (mp_obj_get_float(args[1].u_obj)-value)/(len-1);\n",
    "    else step = (mp_obj_get_float(args[1].u_obj)-value)/len;\n",
    "    ndarray_obj_t *ndarray = create_new_ndarray(1, len, typecode);\n",
    "    if(typecode == NDARRAY_UINT8) {\n",
    "        uint8_t *array = (uint8_t *)ndarray->array->items;\n",
    "        for(size_t i=0; i < len; i++, value += step) array[i] = (uint8_t)value;\n",
    "    } else if(typecode == NDARRAY_INT8) {\n",
    "        int8_t *array = (int8_t *)ndarray->array->items;\n",
    "        for(size_t i=0; i < len; i++, value += step) array[i] = (int8_t)value;\n",
    "    } else if(typecode == NDARRAY_UINT16) {\n",
    "        uint16_t *array = (uint16_t *)ndarray->array->items;\n",
    "        for(size_t i=0; i < len; i++, value += step) array[i] = (uint16_t)value;\n",
    "    } else if(typecode == NDARRAY_INT16) {\n",
    "        int16_t *array = (int16_t *)ndarray->array->items;\n",
    "        for(size_t i=0; i < len; i++, value += step) array[i] = (int16_t)value;\n",
    "    } else {\n",
    "        mp_float_t *array = (mp_float_t *)ndarray->array->items;\n",
    "        for(size_t i=0; i < len; i++, value += step) array[i] = value;\n",
    "    }\n",
    "    if(args[4].u_obj == mp_const_false) {\n",
    "        return MP_OBJ_FROM_PTR(ndarray);\n",
    "    } else {\n",
    "        mp_obj_t tuple[2];\n",
    "        tuple[0] = ndarray;\n",
    "        tuple[1] = mp_obj_new_float(step);\n",
    "        return mp_obj_new_tuple(2, tuple);\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_sum_mean_std_array(mp_obj_t oin, uint8_t optype) {\n",
    "    mp_float_t value, sum = 0.0, sq_sum = 0.0;\n",
    "    mp_obj_iter_buf_t iter_buf;\n",
    "    mp_obj_t item, iterable = mp_getiter(oin, &iter_buf);\n",
    "    mp_int_t len = mp_obj_get_int(mp_obj_len(oin));\n",
    "    while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "        value = mp_obj_get_float(item);\n",
    "        sum += value;\n",
    "        if(optype == NUMERICAL_STD) {\n",
    "            sq_sum += value*value;\n",
    "        }\n",
    "    }\n",
    "    if(optype ==  NUMERICAL_SUM) {\n",
    "        return mp_obj_new_float(sum);\n",
    "    } else if(optype == NUMERICAL_MEAN) {\n",
    "        return mp_obj_new_float(sum/len);\n",
    "    } else {\n",
    "        sum /= len; // this is now the mean!\n",
    "        return mp_obj_new_float(MICROPY_FLOAT_C_FUN(sqrt)(sq_sum/len-sum*sum));\n",
    "    }\n",
    "}\n",
    "\n",
    "STATIC mp_float_t numerical_sum_mean_std_single_line(void *data, size_t start, size_t stop, \n",
    "                                                  size_t stride, uint8_t typecode, uint8_t optype) {\n",
    "    \n",
    "    mp_float_t sum = 0.0, sq_sum = 0.0, value;\n",
    "    size_t len = 0;\n",
    "    for(size_t i=start; i < stop; i+=stride, len++) {\n",
    "        value = ndarray_get_float_value(data, typecode, i);        \n",
    "        sum += value;\n",
    "        if(optype == NUMERICAL_STD) {\n",
    "            sq_sum += value*value;\n",
    "        }\n",
    "    }\n",
    "    if(len == 0) {\n",
    "        mp_raise_ValueError(\"data length is 0!\");\n",
    "    }\n",
    "    if(optype ==  NUMERICAL_SUM) {\n",
    "        return sum;\n",
    "    } else if(optype == NUMERICAL_MEAN) {\n",
    "        return sum/len;\n",
    "    } else {\n",
    "        sum /= len; // this is now the mean!\n",
    "        return MICROPY_FLOAT_C_FUN(sqrt)(sq_sum/len-sum*sum);\n",
    "    }\n",
    "}\n",
    "\n",
    "STATIC mp_obj_t numerical_sum_mean_std_matrix(mp_obj_t oin, mp_obj_t axis, uint8_t optype) {\n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(oin);\n",
    "    if((axis == mp_const_none) || (in->m == 1) || (in->n == 1)) { \n",
    "        // return the value for the flattened array\n",
    "        return mp_obj_new_float(numerical_sum_mean_std_single_line(in->array->items, 0, \n",
    "                                                      in->array->len, 1, in->array->typecode, optype));\n",
    "    } else {\n",
    "        uint8_t _axis = mp_obj_get_int(axis);\n",
    "        size_t m = (_axis == 0) ? 1 : in->m;\n",
    "        size_t n = (_axis == 0) ? in->n : 1;\n",
    "        size_t len = in->array->len;\n",
    "        mp_float_t sms;\n",
    "        // TODO: pass in->array->typcode to create_new_ndarray\n",
    "        ndarray_obj_t *out = create_new_ndarray(m, n, NDARRAY_FLOAT);\n",
    "\n",
    "        // TODO: these two cases could probably be combined in a more elegant fashion...\n",
    "        if(_axis == 0) { // vertical\n",
    "            for(size_t i=0; i < n; i++) {\n",
    "                sms = numerical_sum_mean_std_single_line(in->array->items, i, len, \n",
    "                                                               n, in->array->typecode, optype);\n",
    "                ((float_t *)out->array->items)[i] = sms;\n",
    "            }\n",
    "        } else { // horizontal\n",
    "            for(size_t i=0; i < m; i++) {\n",
    "                sms = numerical_sum_mean_std_single_line(in->array->items, i*in->n, \n",
    "                                                               (i+1)*in->n, 1, in->array->typecode, optype);\n",
    "                ((float_t *)out->array->items)[i] = sms;\n",
    "            }\n",
    "        }\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "    }\n",
    "}\n",
    "\n",
    "size_t numerical_argmin_argmax_array(ndarray_obj_t *in, size_t start, \n",
    "                                       size_t stop, size_t stride, uint8_t op) {\n",
    "    size_t best_idx = start;\n",
    "    if(in->array->typecode == NDARRAY_UINT8) {\n",
    "        ARG_MIN_LOOP(in, uint8_t, start, stop, stride, op);\n",
    "    } else if(in->array->typecode == NDARRAY_INT8) {\n",
    "        ARG_MIN_LOOP(in, int8_t, start, stop, stride, op);\n",
    "    } else if(in->array->typecode == NDARRAY_UINT16) {\n",
    "        ARG_MIN_LOOP(in, uint16_t, start, stop, stride, op);\n",
    "    } else if(in->array->typecode == NDARRAY_INT16) {\n",
    "        ARG_MIN_LOOP(in, uint16_t, start, stop, stride, op);\n",
    "    } else if(in->array->typecode == NDARRAY_FLOAT) {\n",
    "        ARG_MIN_LOOP(in, mp_float_t, start, stop, stride, op);\n",
    "    }\n",
    "    return best_idx;\n",
    "}\n",
    "\n",
    "void copy_value_into_ndarray(ndarray_obj_t *target, ndarray_obj_t *source, size_t target_idx, size_t source_idx) {\n",
    "    // since we are simply copying, it doesn't matter, whether the arrays are signed or unsigned, \n",
    "    // we can cast them in any way we like\n",
    "    // This could also be done with byte copies. I don't know, whether that would have any benefits\n",
    "    if((target->array->typecode == NDARRAY_UINT8) || (target->array->typecode == NDARRAY_INT8)) {\n",
    "        ((uint8_t *)target->array->items)[target_idx] = ((uint8_t *)source->array->items)[source_idx];\n",
    "    } else if((target->array->typecode == NDARRAY_UINT16) || (target->array->typecode == NDARRAY_INT16)) {\n",
    "        ((uint16_t *)target->array->items)[target_idx] = ((uint16_t *)source->array->items)[source_idx];\n",
    "    } else { \n",
    "        ((float *)target->array->items)[target_idx] = ((float *)source->array->items)[source_idx];\n",
    "    }\n",
    "}\n",
    " \n",
    "STATIC mp_obj_t numerical_argmin_argmax(mp_obj_t oin, mp_obj_t axis, uint8_t optype) {\n",
    "    if(MP_OBJ_IS_TYPE(oin, &mp_type_tuple) || MP_OBJ_IS_TYPE(oin, &mp_type_list) || \n",
    "        MP_OBJ_IS_TYPE(oin, &mp_type_range)) {\n",
    "        // This case will work for single iterables only \n",
    "        size_t idx = 0, best_idx = 0;\n",
    "        mp_obj_iter_buf_t iter_buf;\n",
    "        mp_obj_t iterable = mp_getiter(oin, &iter_buf);\n",
    "        mp_obj_t best_obj = MP_OBJ_NULL;\n",
    "        mp_obj_t item;\n",
    "        mp_uint_t op = MP_BINARY_OP_LESS;\n",
    "        if((optype == NUMERICAL_ARGMAX) || (optype == NUMERICAL_MAX)) op = MP_BINARY_OP_MORE;\n",
    "        while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {\n",
    "            if ((best_obj == MP_OBJ_NULL) || (mp_binary_op(op, item, best_obj) == mp_const_true)) {\n",
    "                best_obj = item;\n",
    "                best_idx = idx;\n",
    "            }\n",
    "            idx++;\n",
    "        }\n",
    "        if((optype == NUMERICAL_ARGMIN) || (optype == NUMERICAL_ARGMAX)) {\n",
    "            return MP_OBJ_NEW_SMALL_INT(best_idx);\n",
    "        } else {\n",
    "            return best_obj;\n",
    "        }\n",
    "    } else if(mp_obj_is_type(oin, &ulab_ndarray_type)) {\n",
    "            ndarray_obj_t *in = MP_OBJ_TO_PTR(oin);\n",
    "            size_t best_idx;\n",
    "            if((axis == mp_const_none) || (in->m == 1) || (in->n == 1)) {\n",
    "                // return the value for the flattened array                \n",
    "                best_idx = numerical_argmin_argmax_array(in, 0, in->array->len, 1, optype);\n",
    "                if((optype == NUMERICAL_ARGMIN) || (optype == NUMERICAL_ARGMAX)) {\n",
    "                    return MP_OBJ_NEW_SMALL_INT(best_idx);\n",
    "                } else {\n",
    "                    if(in->array->typecode == NDARRAY_FLOAT) {\n",
    "                        return mp_obj_new_float(ndarray_get_float_value(in->array->items, in->array->typecode, best_idx));\n",
    "                    } else {\n",
    "                        return mp_binary_get_val_array(in->array->typecode, in->array->items, best_idx);\n",
    "                    }\n",
    "                }\n",
    "            } else { // we have to work with a full matrix here\n",
    "                uint8_t _axis = mp_obj_get_int(axis);\n",
    "                size_t m = (_axis == 0) ? 1 : in->m;\n",
    "                size_t n = (_axis == 0) ? in->n : 1;\n",
    "                size_t len = in->array->len;\n",
    "                ndarray_obj_t *ndarray = NULL;\n",
    "                if((optype == NUMERICAL_MAX) || (optype == NUMERICAL_MIN)) {\n",
    "                    ndarray = create_new_ndarray(m, n, in->array->typecode);\n",
    "                } else { // argmin/argmax\n",
    "                    // TODO: one might get away with uint8_t, if both m, and n < 255\n",
    "                    ndarray = create_new_ndarray(m, n, NDARRAY_UINT16);\n",
    "                }\n",
    "\n",
    "                // TODO: these two cases could probably be combined in a more elegant fashion...\n",
    "                if(_axis == 0) { // vertical\n",
    "                    for(size_t i=0; i < n; i++) {\n",
    "                        best_idx = numerical_argmin_argmax_array(in, i, len, n, optype);\n",
    "                        if((optype == NUMERICAL_MIN) || (optype == NUMERICAL_MAX)) {\n",
    "                            copy_value_into_ndarray(ndarray, in, i, best_idx);\n",
    "                        } else {\n",
    "                            ((uint16_t *)ndarray->array->items)[i] = (uint16_t)(best_idx / n);\n",
    "                        }\n",
    "                    }\n",
    "                } else { // horizontal\n",
    "                    for(size_t i=0; i < m; i++) {\n",
    "                        best_idx = numerical_argmin_argmax_array(in, i*in->n, (i+1)*in->n, 1, optype);\n",
    "                        if((optype == NUMERICAL_MIN) || (optype == NUMERICAL_MAX)) {\n",
    "                             copy_value_into_ndarray(ndarray, in, i, best_idx);\n",
    "                        } else {\n",
    "                            ((uint16_t *)ndarray->array->items)[i] = (uint16_t)(best_idx - i*in->n);\n",
    "                        }\n",
    "                    }\n",
    "                }\n",
    "                return MP_OBJ_FROM_PTR(ndarray);\n",
    "            }\n",
    "            return mp_const_none;\n",
    "        }\n",
    "    mp_raise_TypeError(\"input type is not supported\");\n",
    "}\n",
    "\n",
    "STATIC mp_obj_t numerical_function(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args, uint8_t type) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} } ,\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    mp_obj_t oin = args[0].u_obj;\n",
    "    mp_obj_t axis = args[1].u_obj;\n",
    "    if((axis != mp_const_none) && (mp_obj_get_int(axis) != 0) && (mp_obj_get_int(axis) != 1)) {\n",
    "        // this seems to pass with False, and True...\n",
    "        mp_raise_ValueError(\"axis must be None, 0, or 1\");\n",
    "    }\n",
    "    \n",
    "    if(MP_OBJ_IS_TYPE(oin, &mp_type_tuple) || MP_OBJ_IS_TYPE(oin, &mp_type_list) || \n",
    "        MP_OBJ_IS_TYPE(oin, &mp_type_range)) {\n",
    "        switch(type) {\n",
    "            case NUMERICAL_MIN:\n",
    "            case NUMERICAL_ARGMIN:\n",
    "            case NUMERICAL_MAX:\n",
    "            case NUMERICAL_ARGMAX:\n",
    "                return numerical_argmin_argmax(oin, axis, type);\n",
    "            case NUMERICAL_SUM:\n",
    "            case NUMERICAL_MEAN:\n",
    "            case NUMERICAL_STD:\n",
    "                return numerical_sum_mean_std_array(oin, type);\n",
    "            default: // we should never reach this point, but whatever\n",
    "                return mp_const_none;\n",
    "        }\n",
    "    } else if(MP_OBJ_IS_TYPE(oin, &ulab_ndarray_type)) {\n",
    "        switch(type) {\n",
    "            case NUMERICAL_MIN:\n",
    "            case NUMERICAL_MAX:\n",
    "            case NUMERICAL_ARGMIN:\n",
    "            case NUMERICAL_ARGMAX:\n",
    "                return numerical_argmin_argmax(oin, axis, type);\n",
    "            case NUMERICAL_SUM:\n",
    "            case NUMERICAL_MEAN:\n",
    "            case NUMERICAL_STD:\n",
    "                return numerical_sum_mean_std_matrix(oin, axis, type);            \n",
    "            default:\n",
    "                mp_raise_NotImplementedError(\"operation is not implemented on ndarrays\");\n",
    "        }\n",
    "    } else {\n",
    "        mp_raise_TypeError(\"input must be tuple, list, range, or ndarray\");\n",
    "    }\n",
    "    return mp_const_none;\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_min(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_MIN);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_max(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_MAX);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_argmin(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_ARGMIN);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_argmax(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_ARGMAX);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_sum(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_SUM);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_mean(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_MEAN);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_std(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    return numerical_function(n_args, pos_args, kw_args, NUMERICAL_STD);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_roll(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(2, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    mp_obj_t oin = args[0].u_obj;\n",
    "    int16_t shift = mp_obj_get_int(args[1].u_obj);\n",
    "    if((args[2].u_obj != mp_const_none) && \n",
    "           (mp_obj_get_int(args[2].u_obj) != 0) && \n",
    "           (mp_obj_get_int(args[2].u_obj) != 1)) {\n",
    "        mp_raise_ValueError(\"axis must be None, 0, or 1\");\n",
    "    }\n",
    "\n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(oin);\n",
    "    uint8_t _sizeof = mp_binary_get_size('@', in->array->typecode, NULL);\n",
    "    size_t len;\n",
    "    int16_t _shift;\n",
    "    uint8_t *array = (uint8_t *)in->array->items;\n",
    "    // TODO: transpose the matrix, if axis == 0. Though, that is hard on the RAM...\n",
    "    if(shift < 0) {\n",
    "        _shift = -shift;\n",
    "    } else {\n",
    "        _shift = shift;\n",
    "    }\n",
    "    if((args[2].u_obj == mp_const_none) || (mp_obj_get_int(args[2].u_obj) == 1)) { // shift horizontally\n",
    "        uint16_t M;\n",
    "        if(args[2].u_obj == mp_const_none) {\n",
    "            len = in->array->len;\n",
    "            M = 1;\n",
    "        } else {\n",
    "            len = in->n;\n",
    "            M = in->m;\n",
    "        }\n",
    "        _shift = _shift % len;\n",
    "        if(shift < 0) _shift = len - _shift;\n",
    "        // TODO: if(shift > len/2), we should move in the opposite direction. That would save RAM\n",
    "        _shift *= _sizeof;\n",
    "        uint8_t *tmp = m_new(uint8_t, _shift);\n",
    "        for(size_t m=0; m < M; m++) {\n",
    "            memmove(tmp, &array[m*len*_sizeof], _shift);\n",
    "            memmove(&array[m*len*_sizeof], &array[m*len*_sizeof+_shift], len*_sizeof-_shift);\n",
    "            memmove(&array[(m+1)*len*_sizeof-_shift], tmp, _shift);\n",
    "        }\n",
    "        m_del(uint8_t, tmp, _shift);\n",
    "        return mp_const_none;\n",
    "    } else {\n",
    "        len = in->m;\n",
    "        // temporary buffer\n",
    "        uint8_t *_data = m_new(uint8_t, _sizeof*len);\n",
    "        \n",
    "        _shift = _shift % len;\n",
    "        if(shift < 0) _shift = len - _shift;\n",
    "        _shift *= _sizeof;\n",
    "        uint8_t *tmp = m_new(uint8_t, _shift);\n",
    "\n",
    "        for(size_t n=0; n < in->n; n++) {\n",
    "            for(size_t m=0; m < len; m++) {\n",
    "                // this loop should fill up the temporary buffer\n",
    "                memmove(&_data[m*_sizeof], &array[(m*in->n+n)*_sizeof], _sizeof);\n",
    "            }\n",
    "            // now, the actual shift\n",
    "            memmove(tmp, _data, _shift);\n",
    "            memmove(_data, &_data[_shift], len*_sizeof-_shift);\n",
    "            memmove(&_data[len*_sizeof-_shift], tmp, _shift);\n",
    "            for(size_t m=0; m < len; m++) {\n",
    "                // this loop should dump the content of the temporary buffer into data\n",
    "                memmove(&array[(m*in->n+n)*_sizeof], &_data[m*_sizeof], _sizeof);\n",
    "            }            \n",
    "        }\n",
    "        m_del(uint8_t, tmp, _shift);\n",
    "        m_del(uint8_t, _data, _sizeof*len);\n",
    "        return mp_const_none;\n",
    "    }\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_flip(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj)} },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    if(!mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"flip argument must be an ndarray\");\n",
    "    }\n",
    "    if((args[1].u_obj != mp_const_none) && \n",
    "           (mp_obj_get_int(args[1].u_obj) != 0) && \n",
    "           (mp_obj_get_int(args[1].u_obj) != 1)) {\n",
    "        mp_raise_ValueError(\"axis must be None, 0, or 1\");\n",
    "    }\n",
    "\n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(args[0].u_obj);\n",
    "    mp_obj_t oout = ndarray_copy(args[0].u_obj);\n",
    "    ndarray_obj_t *out = MP_OBJ_TO_PTR(oout);\n",
    "    uint8_t _sizeof = mp_binary_get_size('@', in->array->typecode, NULL);\n",
    "    uint8_t *array_in = (uint8_t *)in->array->items;\n",
    "    uint8_t *array_out = (uint8_t *)out->array->items;    \n",
    "    size_t len;\n",
    "    if((args[1].u_obj == mp_const_none) || (mp_obj_get_int(args[1].u_obj) == 1)) { // flip horizontally\n",
    "        uint16_t M = in->m;\n",
    "        len = in->n;\n",
    "        if(args[1].u_obj == mp_const_none) { // flip flattened array\n",
    "            len = in->array->len;\n",
    "            M = 1;\n",
    "        }\n",
    "        for(size_t m=0; m < M; m++) {\n",
    "            for(size_t n=0; n < len; n++) {\n",
    "                memcpy(array_out+_sizeof*(m*len+n), array_in+_sizeof*((m+1)*len-n-1), _sizeof);\n",
    "            }\n",
    "        }\n",
    "    } else { // flip vertically\n",
    "        for(size_t m=0; m < in->m; m++) {\n",
    "            for(size_t n=0; n < in->n; n++) {\n",
    "                memcpy(array_out+_sizeof*(m*in->n+n), array_in+_sizeof*((in->m-m-1)*in->n+n), _sizeof);\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "    return out;\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_diff(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_n, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1 } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = -1 } },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    \n",
    "    if(!mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"diff argument must be an ndarray\");\n",
    "    }\n",
    "    \n",
    "    ndarray_obj_t *in = MP_OBJ_TO_PTR(args[0].u_obj);\n",
    "    size_t increment, N, M;\n",
    "    if((args[2].u_int == -1) || (args[2].u_int == 1)) { // differentiate along the horizontal axis\n",
    "        increment = 1;\n",
    "    } else if(args[2].u_int == 0) { // differtiate along vertical axis\n",
    "        increment = in->n;\n",
    "    } else {\n",
    "        mp_raise_ValueError(\"axis must be -1, 0, or 1\");        \n",
    "    }\n",
    "    if((args[1].u_int < 0) || (args[1].u_int > 9)) {\n",
    "        mp_raise_ValueError(\"n must be between 0, and 9\");\n",
    "    }\n",
    "    uint8_t n = args[1].u_int;\n",
    "    int8_t *stencil = m_new(int8_t, n+1);\n",
    "    stencil[0] = 1;\n",
    "    for(uint8_t i=1; i < n+1; i++) {\n",
    "        stencil[i] = -stencil[i-1]*(n-i+1)/i;\n",
    "    }\n",
    "\n",
    "    ndarray_obj_t *out;\n",
    "    \n",
    "    if(increment == 1) { // differentiate along the horizontal axis \n",
    "        if(n >= in->n) {\n",
    "            out = create_new_ndarray(in->m, 0, in->array->typecode);\n",
    "            m_del(uint8_t, stencil, n);\n",
    "            return MP_OBJ_FROM_PTR(out);\n",
    "        }\n",
    "        N = in->n - n;\n",
    "        M = in->m;\n",
    "    } else { // differentiate along vertical axis\n",
    "        if(n >= in->m) {\n",
    "            out = create_new_ndarray(0, in->n, in->array->typecode);\n",
    "            m_del(uint8_t, stencil, n);\n",
    "            return MP_OBJ_FROM_PTR(out);\n",
    "        }\n",
    "        M = in->m - n;\n",
    "        N = in->n;\n",
    "    }\n",
    "    out = create_new_ndarray(M, N, in->array->typecode);\n",
    "    if(in->array->typecode == NDARRAY_UINT8) {\n",
    "        CALCULATE_DIFF(in, out, uint8_t, M, N, in->n, increment);\n",
    "    } else if(in->array->typecode == NDARRAY_INT8) {\n",
    "        CALCULATE_DIFF(in, out, int8_t, M, N, in->n, increment);\n",
    "    }  else if(in->array->typecode == NDARRAY_UINT16) {\n",
    "        CALCULATE_DIFF(in, out, uint16_t, M, N, in->n, increment);\n",
    "    } else if(in->array->typecode == NDARRAY_INT16) {\n",
    "        CALCULATE_DIFF(in, out, int16_t, M, N, in->n, increment);\n",
    "    } else {\n",
    "        CALCULATE_DIFF(in, out, mp_float_t, M, N, in->n, increment);\n",
    "    }\n",
    "    m_del(int8_t, stencil, n);\n",
    "    return MP_OBJ_FROM_PTR(out);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_sort_helper(mp_obj_t oin, mp_obj_t axis, uint8_t inplace) {\n",
    "    if(!mp_obj_is_type(oin, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"sort argument must be an ndarray\");\n",
    "    }\n",
    "\n",
    "    ndarray_obj_t *ndarray;\n",
    "    mp_obj_t out;\n",
    "    if(inplace == 1) {\n",
    "        ndarray = MP_OBJ_TO_PTR(oin);\n",
    "    } else {\n",
    "        out = ndarray_copy(oin);\n",
    "        ndarray = MP_OBJ_TO_PTR(out);\n",
    "    }\n",
    "    size_t increment, start_inc, end, N;\n",
    "    if(axis == mp_const_none) { // flatten the array\n",
    "        ndarray->m = 1;\n",
    "        ndarray->n = ndarray->array->len;\n",
    "        increment = 1;\n",
    "        start_inc = ndarray->n;\n",
    "        end = ndarray->n;\n",
    "        N = ndarray->n;\n",
    "    } else if((mp_obj_get_int(axis) == -1) || \n",
    "              (mp_obj_get_int(axis) == 1)) { // sort along the horizontal axis\n",
    "        increment = 1;\n",
    "        start_inc = ndarray->n;\n",
    "        end = ndarray->array->len;\n",
    "        N = ndarray->n;\n",
    "    } else if(mp_obj_get_int(axis) == 0) { // sort along vertical axis\n",
    "        increment = ndarray->n;\n",
    "        start_inc = 1;\n",
    "        end = ndarray->m;\n",
    "        N = ndarray->m;\n",
    "    } else {\n",
    "        mp_raise_ValueError(\"axis must be -1, 0, None, or 1\");        \n",
    "    }\n",
    "    \n",
    "    size_t q, k, p, c;\n",
    "\n",
    "    for(size_t start=0; start < end; start+=start_inc) {\n",
    "        q = N; \n",
    "        k = (q >> 1);\n",
    "        if((ndarray->array->typecode == NDARRAY_UINT8) || (ndarray->array->typecode == NDARRAY_INT8)) {\n",
    "            HEAPSORT(uint8_t, ndarray);\n",
    "        } else if((ndarray->array->typecode == NDARRAY_INT16) || (ndarray->array->typecode == NDARRAY_INT16)) {\n",
    "            HEAPSORT(uint16_t, ndarray);\n",
    "        } else {\n",
    "            HEAPSORT(mp_float_t, ndarray);\n",
    "        }\n",
    "    }\n",
    "    if(inplace == 1) {\n",
    "        return mp_const_none;\n",
    "    } else {\n",
    "        return out;\n",
    "    }\n",
    "}\n",
    "\n",
    "// numpy function\n",
    "mp_obj_t numerical_sort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = -1 } },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "\n",
    "    return numerical_sort_helper(args[0].u_obj, args[1].u_obj, 0);\n",
    "}\n",
    "// method of an ndarray\n",
    "mp_obj_t numerical_sort_inplace(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = -1 } },\n",
    "    };\n",
    "\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "\n",
    "    return numerical_sort_helper(args[0].u_obj, args[1].u_obj, 1);\n",
    "}\n",
    "\n",
    "mp_obj_t numerical_argsort(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {\n",
    "    static const mp_arg_t allowed_args[] = {\n",
    "        { MP_QSTR_, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_rom_obj = MP_ROM_PTR(&mp_const_none_obj) } },\n",
    "        { MP_QSTR_axis, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_int = -1 } },\n",
    "    };\n",
    "    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];\n",
    "    mp_arg_parse_all(1, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);\n",
    "    if(!mp_obj_is_type(args[0].u_obj, &ulab_ndarray_type)) {\n",
    "        mp_raise_TypeError(\"argsort argument must be an ndarray\");\n",
    "    }\n",
    "\n",
    "    ndarray_obj_t *ndarray = MP_OBJ_TO_PTR(args[0].u_obj);\n",
    "    size_t increment, start_inc, end, N, m, n;\n",
    "    if(args[1].u_obj == mp_const_none) { // flatten the array\n",
    "        m = 1;\n",
    "        n = ndarray->array->len;\n",
    "        ndarray->m = m;\n",
    "        ndarray->n = n;\n",
    "        increment = 1;\n",
    "        start_inc = ndarray->n;\n",
    "        end = ndarray->n;\n",
    "        N = n;\n",
    "    } else if((mp_obj_get_int(args[1].u_obj) == -1) || \n",
    "              (mp_obj_get_int(args[1].u_obj) == 1)) { // sort along the horizontal axis\n",
    "        m = ndarray->m;\n",
    "        n = ndarray->n;\n",
    "        increment = 1;\n",
    "        start_inc = n;\n",
    "        end = ndarray->array->len;\n",
    "        N = n;\n",
    "    } else if(mp_obj_get_int(args[1].u_obj) == 0) { // sort along vertical axis\n",
    "        m = ndarray->m;\n",
    "        n = ndarray->n;\n",
    "        increment = n;\n",
    "        start_inc = 1;\n",
    "        end = m;\n",
    "        N = m;\n",
    "    } else {\n",
    "        mp_raise_ValueError(\"axis must be -1, 0, None, or 1\");\n",
    "    }\n",
    "\n",
    "    // at the expense of flash, we could save RAM by creating \n",
    "    // an NDARRAY_UINT16 ndarray only, if needed, otherwise, NDARRAY_UINT8\n",
    "    ndarray_obj_t *indices = create_new_ndarray(m, n, NDARRAY_UINT16);\n",
    "    uint16_t *index_array = (uint16_t *)indices->array->items;\n",
    "    // initialise the index array\n",
    "    // if array is flat: 0 to indices->n\n",
    "    // if sorting vertically, identical indices are arranged row-wise\n",
    "    // if sorting horizontally, identical indices are arranged colunn-wise\n",
    "    for(uint16_t start=0; start < end; start+=start_inc) {\n",
    "        for(uint16_t s=0; s < N; s++) {\n",
    "            index_array[start+s*increment] = s;\n",
    "        }\n",
    "    }\n",
    "\n",
    "    size_t q, k, p, c;\n",
    "    for(size_t start=0; start < end; start+=start_inc) {\n",
    "        q = N; \n",
    "        k = (q >> 1);\n",
    "        if((ndarray->array->typecode == NDARRAY_UINT8) || (ndarray->array->typecode == NDARRAY_INT8)) {\n",
    "            HEAP_ARGSORT(uint8_t, ndarray, index_array);\n",
    "        } else if((ndarray->array->typecode == NDARRAY_INT16) || (ndarray->array->typecode == NDARRAY_INT16)) {\n",
    "            HEAP_ARGSORT(uint16_t, ndarray, index_array);\n",
    "        } else {\n",
    "            HEAP_ARGSORT(mp_float_t, ndarray, index_array);\n",
    "        }\n",
    "    }\n",
    "    return MP_OBJ_FROM_PTR(indices);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ulab module\n",
    "\n",
    "This module simply brings all components together, and does not contain new function definitions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ulab.c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-29T07:00:45.792518Z",
     "start_time": "2019-11-29T07:00:45.787301Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "written 9752 bytes to ulab.c\n"
     ]
    }
   ],
   "source": [
    "%%ccode ulab.c\n",
    "\n",
    "#include <math.h>\n",
    "#include <stdio.h>\n",
    "#include <stdlib.h>\n",
    "#include <string.h>\n",
    "#include \"py/runtime.h\"\n",
    "#include \"py/binary.h\"\n",
    "#include \"py/obj.h\"\n",
    "#include \"py/objarray.h\"\n",
    "\n",
    "#include \"ndarray.h\"\n",
    "#include \"linalg.h\"\n",
    "#include \"vectorise.h\"\n",
    "#include \"poly.h\"\n",
    "#include \"fft.h\"\n",
    "#include \"numerical.h\"\n",
    "\n",
    "#define ULAB_VERSION 0.262\n",
    "\n",
    "typedef struct _mp_obj_float_t {\n",
    "    mp_obj_base_t base;\n",
    "    mp_float_t value;\n",
    "} mp_obj_float_t;\n",
    "\n",
    "mp_obj_float_t ulab_version = {{&mp_type_float}, ULAB_VERSION};\n",
    "\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(ndarray_shape_obj, ndarray_shape);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(ndarray_rawsize_obj, ndarray_rawsize);\n",
    "MP_DEFINE_CONST_FUN_OBJ_KW(ndarray_flatten_obj, 1, ndarray_flatten);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(ndarray_asbytearray_obj, ndarray_asbytearray);\n",
    "\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(linalg_transpose_obj, linalg_transpose);\n",
    "MP_DEFINE_CONST_FUN_OBJ_2(linalg_reshape_obj, linalg_reshape);\n",
    "MP_DEFINE_CONST_FUN_OBJ_KW(linalg_size_obj, 1, linalg_size);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(linalg_inv_obj, linalg_inv);\n",
    "MP_DEFINE_CONST_FUN_OBJ_2(linalg_dot_obj, linalg_dot);\n",
    "MP_DEFINE_CONST_FUN_OBJ_KW(linalg_zeros_obj, 0, linalg_zeros);\n",
    "MP_DEFINE_CONST_FUN_OBJ_KW(linalg_ones_obj, 0, linalg_ones);\n",
    "MP_DEFINE_CONST_FUN_OBJ_KW(linalg_eye_obj, 0, linalg_eye);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(linalg_det_obj, linalg_det);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(linalg_eig_obj, linalg_eig);\n",
    "\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_acos_obj, vectorise_acos);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_acosh_obj, vectorise_acosh);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_asin_obj, vectorise_asin);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_asinh_obj, vectorise_asinh);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_atan_obj, vectorise_atan);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_atanh_obj, vectorise_atanh);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_ceil_obj, vectorise_ceil);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_cos_obj, vectorise_cos);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_erf_obj, vectorise_erf);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_erfc_obj, vectorise_erfc);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_exp_obj, vectorise_exp);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_expm1_obj, vectorise_expm1);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_floor_obj, vectorise_floor);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_gamma_obj, vectorise_gamma);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_lgamma_obj, vectorise_lgamma);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log_obj, vectorise_log);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log10_obj, vectorise_log10);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_log2_obj, vectorise_log2);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sin_obj, vectorise_sin);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sinh_obj, vectorise_sinh);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_sqrt_obj, vectorise_sqrt);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_tan_obj, vectorise_tan);\n",
    "MP_DEFINE_CONST_FUN_OBJ_1(vectorise_tanh_obj, vectorise_tanh);\n",
    "\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_linspace_obj, 2, numerical_linspace);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_sum_obj, 1, numerical_sum);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_mean_obj, 1, numerical_mean);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_std_obj, 1, numerical_std);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_min_obj, 1, numerical_min);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_max_obj, 1, numerical_max);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_argmin_obj, 1, numerical_argmin);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_argmax_obj, 1, numerical_argmax);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_roll_obj, 2, numerical_roll);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_flip_obj, 1, numerical_flip);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_diff_obj, 1, numerical_diff);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_sort_obj, 1, numerical_sort);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_sort_inplace_obj, 1, numerical_sort_inplace);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_KW(numerical_argsort_obj, 1, numerical_argsort);\n",
    "\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_2(poly_polyval_obj, poly_polyval);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(poly_polyfit_obj, 2, 3, poly_polyfit);\n",
    "\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_fft_obj, 1, 2, fft_fft);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_ifft_obj, 1, 2, fft_ifft);\n",
    "STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(fft_spectrum_obj, 1, 2, fft_spectrum);\n",
    "\n",
    "STATIC const mp_rom_map_elem_t ulab_ndarray_locals_dict_table[] = {\n",
    "    { MP_ROM_QSTR(MP_QSTR_shape), MP_ROM_PTR(&ndarray_shape_obj) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_rawsize), MP_ROM_PTR(&ndarray_rawsize_obj) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_flatten), MP_ROM_PTR(&ndarray_flatten_obj) },    \n",
    "    { MP_ROM_QSTR(MP_QSTR_asbytearray), MP_ROM_PTR(&ndarray_asbytearray_obj) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_transpose), MP_ROM_PTR(&linalg_transpose_obj) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_reshape), MP_ROM_PTR(&linalg_reshape_obj) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_sort), MP_ROM_PTR(&numerical_sort_inplace_obj) },\n",
    "};\n",
    "\n",
    "STATIC MP_DEFINE_CONST_DICT(ulab_ndarray_locals_dict, ulab_ndarray_locals_dict_table);\n",
    "\n",
    "const mp_obj_type_t ulab_ndarray_type = {\n",
    "    { &mp_type_type },\n",
    "    .name = MP_QSTR_ndarray,\n",
    "    .print = ndarray_print,\n",
    "    .make_new = ndarray_make_new,\n",
    "    .subscr = ndarray_subscr,\n",
    "    .getiter = ndarray_getiter,\n",
    "    .unary_op = ndarray_unary_op,\n",
    "    .binary_op = ndarray_binary_op,\n",
    "    .locals_dict = (mp_obj_dict_t*)&ulab_ndarray_locals_dict,\n",
    "};\n",
    "\n",
    "STATIC const mp_map_elem_t ulab_globals_table[] = {\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_ulab) },\n",
    "    { MP_ROM_QSTR(MP_QSTR___version__), MP_ROM_PTR(&ulab_version) },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_array), (mp_obj_t)&ulab_ndarray_type },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_size), (mp_obj_t)&linalg_size_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_inv), (mp_obj_t)&linalg_inv_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_dot), (mp_obj_t)&linalg_dot_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_zeros), (mp_obj_t)&linalg_zeros_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_ones), (mp_obj_t)&linalg_ones_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_eye), (mp_obj_t)&linalg_eye_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_det), (mp_obj_t)&linalg_det_obj },\n",
    "    { MP_ROM_QSTR(MP_QSTR_eig), (mp_obj_t)&linalg_eig_obj },    \n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_acos), (mp_obj_t)&vectorise_acos_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_acosh), (mp_obj_t)&vectorise_acosh_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_asin), (mp_obj_t)&vectorise_asin_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_asinh), (mp_obj_t)&vectorise_asinh_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_atan), (mp_obj_t)&vectorise_atan_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_atanh), (mp_obj_t)&vectorise_atanh_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_ceil), (mp_obj_t)&vectorise_ceil_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_cos), (mp_obj_t)&vectorise_cos_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_erf), (mp_obj_t)&vectorise_erf_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_erfc), (mp_obj_t)&vectorise_erfc_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_exp), (mp_obj_t)&vectorise_exp_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_expm1), (mp_obj_t)&vectorise_expm1_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_floor), (mp_obj_t)&vectorise_floor_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_gamma), (mp_obj_t)&vectorise_gamma_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_lgamma), (mp_obj_t)&vectorise_lgamma_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_log), (mp_obj_t)&vectorise_log_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_log10), (mp_obj_t)&vectorise_log10_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_log2), (mp_obj_t)&vectorise_log2_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_sin), (mp_obj_t)&vectorise_sin_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_sinh), (mp_obj_t)&vectorise_sinh_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_sqrt), (mp_obj_t)&vectorise_sqrt_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_tan), (mp_obj_t)&vectorise_tan_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_tanh), (mp_obj_t)&vectorise_tanh_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_linspace), (mp_obj_t)&numerical_linspace_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_sum), (mp_obj_t)&numerical_sum_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_mean), (mp_obj_t)&numerical_mean_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_std), (mp_obj_t)&numerical_std_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_min), (mp_obj_t)&numerical_min_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_max), (mp_obj_t)&numerical_max_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_argmin), (mp_obj_t)&numerical_argmin_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_argmax), (mp_obj_t)&numerical_argmax_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_roll), (mp_obj_t)&numerical_roll_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_flip), (mp_obj_t)&numerical_flip_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_diff), (mp_obj_t)&numerical_diff_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_sort), (mp_obj_t)&numerical_sort_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_argsort), (mp_obj_t)&numerical_argsort_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_polyval), (mp_obj_t)&poly_polyval_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_polyfit), (mp_obj_t)&poly_polyfit_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_fft), (mp_obj_t)&fft_fft_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_ifft), (mp_obj_t)&fft_ifft_obj },\n",
    "    { MP_OBJ_NEW_QSTR(MP_QSTR_spectrum), (mp_obj_t)&fft_spectrum_obj },\n",
    "    // class constants\n",
    "    { MP_ROM_QSTR(MP_QSTR_uint8), MP_ROM_INT(NDARRAY_UINT8) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_int8), MP_ROM_INT(NDARRAY_INT8) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_uint16), MP_ROM_INT(NDARRAY_UINT16) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_int16), MP_ROM_INT(NDARRAY_INT16) },\n",
    "    { MP_ROM_QSTR(MP_QSTR_float), MP_ROM_INT(NDARRAY_FLOAT) },\n",
    "};\n",
    "\n",
    "STATIC MP_DEFINE_CONST_DICT (\n",
    "    mp_module_ulab_globals,\n",
    "    ulab_globals_table\n",
    ");\n",
    "\n",
    "const mp_obj_module_t ulab_user_cmodule = {\n",
    "    .base = { &mp_type_module },\n",
    "    .globals = (mp_obj_dict_t*)&mp_module_ulab_globals,\n",
    "};\n",
    "\n",
    "MP_REGISTER_MODULE(MP_QSTR_ulab, ulab_user_cmodule, MODULE_ULAB_ENABLED);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## makefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 411,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-01T20:40:54.724840Z",
     "start_time": "2019-11-01T20:40:54.719519Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ../../../ulab/code/micropython.mk\n"
     ]
    }
   ],
   "source": [
    "%%writefile ../../../ulab/code/micropython.mk\n",
    "\n",
    "USERMODULES_DIR := $(USERMOD_DIR)\n",
    "\n",
    "# Add all C files to SRC_USERMOD.\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/ndarray.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/linalg.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/vectorise.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/poly.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/fft.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/numerical.c\n",
    "SRC_USERMOD += $(USERMODULES_DIR)/ulab.c\n",
    "\n",
    "# We can add our module folder to include paths if needed\n",
    "# This is not actually needed in this example.\n",
    "CFLAGS_USERMOD += -I$(USERMODULES_DIR)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## make"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### unix port"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-28T18:02:23.457395Z",
     "start_time": "2019-11-28T18:02:23.452052Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/v923z/sandbox/micropython/v1.11/micropython/ports/unix\n"
     ]
    }
   ],
   "source": [
    "%cd ../../../micropython/ports/unix/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-29T06:59:01.155825Z",
     "start_time": "2019-11-29T06:59:00.779150Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.\n",
      "rm -f micropython\n",
      "rm -f micropython.map\n",
      "rm -rf build \n"
     ]
    }
   ],
   "source": [
    "!make clean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-29T06:59:30.206401Z",
     "start_time": "2019-11-29T06:59:03.404517Z"
    },
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "!make USER_C_MODULES=../../../ulab all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### stm32 port"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 162,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-06T06:43:00.293634Z",
     "start_time": "2019-11-06T06:43:00.288195Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/v923z/sandbox/micropython/v1.11/micropython/ports/stm32\n"
     ]
    }
   ],
   "source": [
    "%cd ../../../micropython/ports/stm32/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-14T19:28:28.364859Z",
     "start_time": "2019-11-14T19:28:02.175085Z"
    },
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "!make BOARD=PYBV11 USER_C_MODULES=../../../ulab all"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Change log"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2019-11-29T07:01:25.832268Z",
     "start_time": "2019-11-29T07:01:25.825600Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting ../../../ulab/docs/ulab-change-log.md\n"
     ]
    }
   ],
   "source": [
    "%%writefile ../../../ulab/docs/ulab-change-log.md\n",
    "\n",
    "Fri, 29 Nov 2019\n",
    "\n",
    "version 0.262\n",
    "\n",
    "    fixed error in macro in vectorise.h\n",
    "\n",
    "Thu, 28 Nov 2019\n",
    "\n",
    "version 0.261\n",
    "\n",
    "    fixed bad indexing error in linalg.dot\n",
    "\n",
    "Tue, 6 Nov 2019\n",
    "\n",
    "version 0.26\n",
    "\n",
    "    added in-place sorting (method of ndarray), and argsort\n",
    "    \n",
    "Mon, 4 Nov 2019\n",
    "\n",
    "version 0.25\n",
    "\n",
    "    added first implementation of sort, and fixed section on compiling the module in the manual\n",
    "\n",
    "Thu, 31 Oct 2019\n",
    "\n",
    "version 0.24\n",
    "\n",
    "    added diff to numerical.c\n",
    "    \n",
    "Tue, 29 Oct 2019\n",
    "\n",
    "version 0.23\n",
    "\n",
    "    major revamp of subscription method\n",
    "\n",
    "Sat, 19 Oct 2019\n",
    "\n",
    "version 0.21\n",
    "\n",
    "    fixed trivial bug in .rawsize()\n",
    "\n",
    "Sat, 19 Oct 2019\n",
    "\n",
    "version 0.22\n",
    "\n",
    "    fixed small error in linalg_det, and implemented linalg_eig.\n",
    "\n",
    "\n",
    "Thu, 17 Oct 2019\n",
    "\n",
    "version 0.21\n",
    "\n",
    "    implemented uniform interface for fft, and spectrum, and added ifft.\n",
    "\n",
    "Wed, 16 Oct 2019\n",
    "\n",
    "version 0.20\n",
    "\n",
    "    Added flip function to numerical.c, and moved the size function to linalg. In addition, \n",
    "    size is a function now, and not a method.\n",
    "\n",
    "Tue, 15 Oct 2019\n",
    "\n",
    "version 0.19\n",
    "\n",
    "    fixed roll in numerical.c: it can now accept the axis=None keyword argument, added determinant to linalg.c\n",
    "\n",
    "Mon, 14 Oct 2019\n",
    "\n",
    "version 0.18\n",
    "\n",
    "    fixed min/man function in numerical.c; it conforms to numpy behaviour\n",
    "\n",
    "Fri, 11 Oct 2019\n",
    "\n",
    "version 0.171\n",
    "\n",
    "    found and fixed small bux in roll function\n",
    "\n",
    "Fri, 11 Oct 2019\n",
    "\n",
    "version 0.17\n",
    "\n",
    "    universal function can now take arbitrary typecodes\n",
    "\n",
    "Fri, 11 Oct 2019\n",
    "\n",
    "version 0.161\n",
    "\n",
    "    fixed bad error in iterator, and make_new_ndarray \n",
    "    \n",
    "Thu, 10 Oct 2019\n",
    "\n",
    "varsion 0.16\n",
    "\n",
    "    changed ndarray to array in ulab.c, so as to conform to numpy's notation\n",
    "    extended subscr method to include slices (partially works)\n",
    "    \n",
    "Tue, 8 Oct 2019\n",
    "\n",
    "version 0.15\n",
    "\n",
    "    added inv, neg, pos, and abs unary operators to ndarray.c\n",
    "    \n",
    "Mon, 7 Oct 2019\n",
    "\n",
    "version 0.14\n",
    "\n",
    "    made the internal binary_op function tighter, and added keyword arguments to linspace\n",
    "    \n",
    "Sat, 4 Oct 2019\n",
    "\n",
    "version 0.13\n",
    "\n",
    "    added the <, <=, >, >= binary operators to ndarray\n",
    "\n",
    "Fri, 4 Oct 2019\n",
    "\n",
    "version 0.12\n",
    "\n",
    "    added .flatten to ndarray, ones, zeros, and eye to linalg\n",
    "\n",
    "Thu, 3 Oct 2019\n",
    "\n",
    "version 0.11\n",
    "    \n",
    "    binary operators are now based on macros"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "382.797px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  },
  "toc-autonumbering": true,
  "toc-showcode": false,
  "toc-showmarkdowntxt": true,
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
